views:

102

answers:

2
public interface ITest {
   void Somethink();
}

public class Test1 : ITest {
   public void Somethink()  { /* do stuff */ }
   public int Test1Property { get; set; }
}

public class Test2 : ITest {
   public void Somethink()  { /* do stuff */ }
   public float Test2Property { get; set; }
}

//Main class
public class MainClass
{
   [TypeConverter(ExpandableObjectConverter)]
   public ITest test { get; set; }
}

Ok, i have sth like this. Instance of MainClass is selected by PropertyGrid.

How to make a DropDownList of objects of classes which implement ITest (here Test1 and Test2)

+1  A: 

That's not how it works. The test property getter will return an object of a concrete class that implements ITest. Whatever was assigned to it last, either null, an object of Test1 or an object of Test2. PropertyGrid uses Reflection to look at the object Type and its members. It will display either Test1Property or Test2Property. You cannot choose.

Not sure what you are trying to do, you probably want a UITypeEditor if you want to assign an object of a different type.

Hans Passant
I'll check how UITypeEditor works
nilphilus
A: 

Ok, I used UITypeEditor (thx nobugz), and create combobox for possible values. The values i get from Type[] BehaviorManager.GetBehaviorsWhichImplement(Type type) - that return an array of types implementing given interface.

When user have select a new value, i get a new instance of selected Object BehaviorManager.GetBehavior(Type) which using a Activator.CreateInstance. And assign it to Property.

Of course, it isn't a dropdownlist, but it's pretty good too :-)

here is a article which i following - http://philwinkel.com/blog/?p=4

I know, my grammer is tragic, sorry, i'm still trying to do sth with this ;-)

nilphilus