Jon Skeet [C# MVP] ha scritto:
Try http://www.yoda.arachsys.com/csharp/complete.html - pobox.com is
just a redirector.
The redirector is OK, its yoda the "health" site. Anyway, in the
evening the damn thing is down, so I was able to reach your page. It's
always nice to meet another Catan's settler.
This is exactly what I'm trying to do:
I've got the following assemblies:
------------------------------------------------------------------------------------
the assembly MyBaseClass defining just the following class:
using System;
namespace MyBaseClass
{
public class BaseClass
{
public BaseClass()
{
}
}
}
------------------------------------------------------------------------------------
the assembly MyDerivedClasses which references MyBaseClass and contains
2 derived classes: DerivedA and DerivedB:
using System;
using MyBaseClass;
namespace MyDerivedClasses
{
public class DerivedA : BaseClass
{
public DerivedA()
{
}
}
public class DerivedB : BaseClass
{
public DerivedB()
{
}
}
}
--------------------------------------------------------------------------------------------------
the assembly MyTypeEditor, which references MyBaseClass and the System
assemblies System, System.Drawing.Design, System.Windows.Forms.Design,
System.Windows.Forms, System.ComponentModel.Design, System.Reflection,
EnvDTE, VSLangProj.
This assembly defines MyEditor as follows:
using System;
using System.Drawing.Design;
using System.Windows.Forms.Design;
using System.Windows.Forms;
using System.ComponentModel.Design;
using System.Reflection;
using EnvDTE;
using VSLangProj;
using MyBaseClass;
namespace MyTypeEditor
{
public class MyEditor : UITypeEditor
{
IWindowsFormsEditorService edSvc;
ListBox lista;
public MyEditor()
{
}
public override UITypeEditorEditStyle GetEditStyle(
System.ComponentModel.ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.DropDown;
}
public override object
EditValue(System.ComponentModel.ITypeDescriptorCon text context,
System.IServiceProvider provider, object value)
{
ITypeResolutionService rs;
DTE currentDTE;
edSvc
=(IWindowsFormsEditorService)provider.GetService(t ypeof(IWindowsFormsEditorService));
rs =
(ITypeResolutionService)context.GetService(typeof( ITypeResolutionService));
currentDTE = (DTE)context.GetService(typeof(DTE));
Solution sol = currentDTE.Solution;
lista=new ListBox();
foreach (Project prj in sol.Projects)
{
VSProject vsPrj=(VSProject) prj.Object;
References refs = vsPrj.References;
foreach (Reference aRef in refs)
{
if (!aRef.Name.StartsWith("System"))
{
AssemblyName aName=new AssemblyName();
aName.Name=aRef.Name;
Assembly a=rs.GetAssembly(aName);
if (a!=null)
{
Type[] types=a.GetTypes();
foreach (Type t in types)
{// this MessageBox added to
understand what's happening at design time The problem occurs at this
point
MessageBox.Show(t.Name +" is
derived from BaseClass: " +
t.IsSubclassOf(typeof(BaseClass)).ToString());
if
(t.IsSubclassOf(typeof(BaseClass)))
lista.Items.Add(t.FullName);
}
}
}
}
}
try
{
lista.SelectedIndexChanged +=new
EventHandler(lista_SelectedIndexChanged);
edSvc.DropDownControl(lista);
return lista.SelectedItem;
}
catch
{
return value;
}
finally
{
lista.Dispose();
}
return value;
}
private void lista_SelectedIndexChanged(object sender,
EventArgs e)
{
if (edSvc != null) edSvc.CloseDropDown();
}
}
}
-----------------------------------------------------------------------------------------------------------
Finally there is the assembly MyTestControl which defines the
TestControl UserControl (a very simple control containing only a
TextBox and a string property which uses MyEditor as UITypeEditor) and
references MyTypeEditor:
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Drawing.Design;
using TestListConverter;
namespace MyTestControl
{
public class TestControl : System.Windows.Forms.UserControl
{
private String ps;
[Editor(typeof(UnEditor),typeof(UITypeEditor))]
public String s
{
get {return ps;}
set {ps = value;}
}
private System.Windows.Forms.TextBox textBox1;
private System.ComponentModel.Container components = null;
public TestControl()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if( components != null )
components.Dispose();
}
base.Dispose( disposing );
}
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
this.textBox1.Location = new System.Drawing.Point(24,
16);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
this.Controls.Add(this.textBox1);
this.Name = "TestControl";
this.Size = new System.Drawing.Size(144, 56);
this.ResumeLayout(false);
}
}
}
---------------------------------------------------------------------------------------------------
At this point I open a new Windows Application Project with references
to MyBaseClass and MyTestControl. At this point if I FIRST add a
reference to MyDerivedClasses, and THEN add a TestControl to the form
everything seems to work fine when I try to set te s property of the
control using the editor. If I change the order of these steps (i.e. I
FIRST add the control, and THEN the reference) DerivedA and DerivedB
appear not to be derived from BaseClass.
Any idea?
Thanks again.
Francesco.