修改 PropertyGrid 中的 CollectionEditor

发布于 2024-08-24 05:24:26 字数 935 浏览 6 评论 0原文

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

   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; }
}

我的问题是,无论如何要移动标记包含列表可以包含的所有可能类型的 Type[] 的位置?我想将以下内容添加到我的 CustomCollectionEditor 类中,但这不起作用。

public CustomCollectionEditor(Type type, List<Type> types_)
  : base(type)
{
  types = types_.ToArray();
}

如果我能够以某种方式在 DisplayList 类中标记 CustomCollectionEditor 需要了解哪些类,那就太理想了。

I currently have a list containing Call's, which is the base class. If I want to add derived classes of Call to the list, I know to do the following.

   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; }
}

My questions is there anyway of moving where you mark up the Type[] containing all possible types the list can contain? I thought of adding the following to my CustomCollectionEditor class, but this doesn't work.

public CustomCollectionEditor(Type type, List<Type> types_)
  : base(type)
{
  types = types_.ToArray();
}

It would be ideal if I could mark up which classes the CustomCollectionEditor needed to be aware of in the DisplayList class somehow.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

闻呓 2024-08-31 05:24:26

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

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;
  }

Sure, you can get all types by using reflection.

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;
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文