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; }
}好的,我有这样的东西。PropertyGrid选择MainClass的实例。
如何创建实现ITest的类的对象的DropDownList (这里是Test1和Test2)
发布于 2010-04-02 10:59:49
这不是它的工作方式。测试属性getter将返回实现ITest的具体类的对象。无论最后分配给它的是什么,都是null、Test1的对象或Test2的对象。PropertyGrid使用反射来查看对象类型及其成员。它将显示Test1Property或Test2Property。你不能选择。
不确定您正在尝试做什么,如果您想要分配一个不同类型的对象,您可能需要一个UITypeEditor。
发布于 2010-04-04 01:22:10
好的,我使用了UITypeEditor (thx nobugz),并为可能的值创建了组合框。我从Type[] BehaviorManager.GetBehaviorsWhichImplement(Type type)得到的值,它返回一个实现给定接口的类型数组。
当用户选择了一个新值时,我得到了一个使用Activator.CreateInstance的选定对象BehaviorManager.GetBehavior(Type)的新实例。并将其分配给Property。
当然,它不是一个下拉列表,但它也相当不错:-)
下面是我关注的一篇文章-- http://philwinkel.com/blog/?p=4
我知道,我的语法是悲剧性的,对不起,我还在尝试做一些事情;-)
https://stackoverflow.com/questions/2564754
复制相似问题