C# 在PropertyGrid中修改CollectionEditor

C# 在PropertyGrid中修改CollectionEditor,c#,collectioneditor,C#,Collectioneditor,我目前有一个包含调用的列表,它是基类。如果我想将调用的派生类添加到列表中,我知道要执行以下操作 public class CustomCollectionEditor : System.ComponentModel.Design.CollectionEditor { private Type[] types; public CustomCollectionEditor(Type type) : base(type) {

我目前有一个包含调用的列表,它是基类。如果我想将调用的派生类添加到列表中,我知道要执行以下操作

   public class CustomCollectionEditor : System.ComponentModel.Design.CollectionEditor
    {
      private Type[] types;
      public CustomCollectionEditor(Type type)
        : base(type)
      {
        types = new Type[] { typeof(Call), typeof(CappedCall) };
      }

  protected override Type[] CreateNewItemTypes()
  {
    return types;
  }
}

public class DisplayList
{
  public DisplayList() { }
  [Editor(typeof(CustomCollectionEditor), typeof(UITypeEditor))]
  [DataMember] public List<Call> ListCalls { get; set; }
}
公共类CustomCollectionEditor:System.ComponentModel.Design.CollectionEditor
{
私有类型[]类型;
公共CustomCollectionEditor(类型)
:基本(类型)
{
types=新类型[]{typeof(Call),typeof(CappedCall)};
}
受保护的覆盖类型[]CreateNewItemTypes()
{
返回类型;
}
}
公共类显示列表
{
公共显示列表(){}
[编辑器(typeof(CustomCollectionEditor)、typeof(UITypeEditor))]
[DataMember]公共列表ListCalls{get;set;}
}
我的问题是,是否要移动到包含列表中可能包含的所有类型的类型[]的标记位置?我曾想过将以下内容添加到CustomCollectionEditor类中,但这不起作用

public CustomCollectionEditor(Type type, List<Type> types_)
  : base(type)
{
  types = types_.ToArray();
}
公共CustomCollectionEditor(类型、列表类型) :基本(类型) { types=types.ToArray(); }
如果我能以某种方式在DisplayList类中标记CustomCollectionEditor需要注意的类,那将是一个理想的选择。

当然,您可以通过使用反射获得所有类型

private Type[] types;
private Type itemType;

public CustomCollectionEditor(Type type) {
   itemType = t.GetProperty("Item").PropertyType;
   types = GetTypes();
}

private Type[] GetTypes() {
            List<Type> tList = new List<Type>();
            Assembly[] appAssemblies = AppDomain.CurrentDomain.GetAssemblies();
            foreach (Assembly a in appAssemblies) {
                Module[] mod = a.GetModules();
                foreach (Module m in mod) {
                    Type[] types = m.GetTypes();
                    foreach (Type t in types) {
                        try {
                            if (/*t.Namespace == "Mynamespace.tofilter" && */!t.IsAbstract) {
                                /* Here you should find a better way to cover all Basetypes in the inheritance tree */
                                if (t.BaseType == itemType || t.BaseType.BaseType == itemType) { 
                                    tList.Add(t);
                                }
                            }
                        }
                        catch (NullReferenceException) { }
                    }
                }
            }
            return tList.ToArray();
        }


  protected override Type[] CreateNewItemTypes()
  {
    return types;
  }
私有类型[]类型;
私有类型itemType;
公共CustomCollectionEditor(类型){
itemType=t.GetProperty(“项目”).PropertyType;
types=GetTypes();
}
私有类型[]GetTypes(){
List tList=新列表();
程序集[]appAssemblies=AppDomain.CurrentDomain.GetAssemblies();
foreach(appAssemblies中的组件a){
模块[]mod=a.GetModules();
foreach(模块m在mod中){
Type[]types=m.GetTypes();
foreach(类型中的类型t){
试一试{
if(/*t.Namespace==“Mynamespace.tofilter”&&*/!t.isastract){
/*在这里,您应该找到一种更好的方法来覆盖继承树中的所有基类型*/
如果(t.BaseType==itemType | | t.BaseType.BaseType==itemType){
t增加(t);
}
}
}
捕获(NullReferenceException){}
}
}
}
返回tList.ToArray();
}
受保护的覆盖类型[]CreateNewItemTypes()
{
返回类型;
}

当然,可以使用反射获取所有类型

private Type[] types;
private Type itemType;

public CustomCollectionEditor(Type type) {
   itemType = t.GetProperty("Item").PropertyType;
   types = GetTypes();
}

private Type[] GetTypes() {
            List<Type> tList = new List<Type>();
            Assembly[] appAssemblies = AppDomain.CurrentDomain.GetAssemblies();
            foreach (Assembly a in appAssemblies) {
                Module[] mod = a.GetModules();
                foreach (Module m in mod) {
                    Type[] types = m.GetTypes();
                    foreach (Type t in types) {
                        try {
                            if (/*t.Namespace == "Mynamespace.tofilter" && */!t.IsAbstract) {
                                /* Here you should find a better way to cover all Basetypes in the inheritance tree */
                                if (t.BaseType == itemType || t.BaseType.BaseType == itemType) { 
                                    tList.Add(t);
                                }
                            }
                        }
                        catch (NullReferenceException) { }
                    }
                }
            }
            return tList.ToArray();
        }


  protected override Type[] CreateNewItemTypes()
  {
    return types;
  }
私有类型[]类型;
私有类型itemType;
公共CustomCollectionEditor(类型){
itemType=t.GetProperty(“项目”).PropertyType;
types=GetTypes();
}
私有类型[]GetTypes(){
List tList=新列表();
程序集[]appAssemblies=AppDomain.CurrentDomain.GetAssemblies();
foreach(appAssemblies中的组件a){
模块[]mod=a.GetModules();
foreach(模块m在mod中){
Type[]types=m.GetTypes();
foreach(类型中的类型t){
试一试{
if(/*t.Namespace==“Mynamespace.tofilter”&&*/!t.isastract){
/*在这里,您应该找到一种更好的方法来覆盖继承树中的所有基类型*/
如果(t.BaseType==itemType | | t.BaseType.BaseType==itemType){
t增加(t);
}
}
}
捕获(NullReferenceException){}
}
}
}
返回tList.ToArray();
}
受保护的覆盖类型[]CreateNewItemTypes()
{
返回类型;
}