代码之家  ›  专栏  ›  技术社区  ›  nilphilus

PropertyGrid+接口

  •  0
  • nilphilus  · 技术社区  · 14 年前
    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)

    2 回复  |  直到 14 年前
        1
  •  1
  •   Hans Passant    14 年前

    不是这样的。测试属性getter将返回实现ITest的具体类的对象。不管最后分配给它什么,要么是null,要么是Test1的对象,要么是Test2的对象。PropertyGrid使用反射查看对象类型及其成员。它将显示Test1Property或Test2Property。你不能选择。

    不确定要执行什么操作,如果要指定不同类型的对象,可能需要UITypeEditor。

        2
  •  0
  •   nilphilus    14 年前

    好的,我使用UITypeEditor(thx nobugz),并为可能的值创建组合框。我从中得到的价值观 Type[] BehaviorManager.GetBehaviorsWhichImplement(Type type) -返回实现给定接口的类型数组。

    当用户选择了一个新值时,我得到了一个新的实例 BehaviorManager.GetBehavior(Type) 它使用Activator.CreateInstance。并将其分配给财产。

    下面是我要写的一篇文章- http://philwinkel.com/blog/?p=4

    我知道,我的语法是悲惨的,对不起,我仍然试图用这个做某事;-)