1

私は現在、基本クラスである 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();
}

CustomCollectionEditor がどのクラスを認識する必要があるかを DisplayList クラスで何らかの方法でマークアップできれば理想的です。

4

1 に答える 1

1

もちろん、リフレクションを使用してすべての型を取得できます。

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;
  }
于 2010-03-26T07:27:18.723 に答える