簡體   English   中英

如何為每個類避免使用單獨的自定義TypeDescriptorProvider?

[英]How can I avoid having a separate custom TypeDescriptorProvider for each of my classes?

我在項目中添加了一些功能,允許用戶將自己的自定義屬性添加到對象中。 我已經創建了自己的自定義TypeDescriptorPropertyDescriptorTypeDescriptorProviders等等。要做到這一點。

這是我的問題。 現在我已經完成了所有工作,但必須為每個可以擁有自定義屬性的對象對象類型創建單獨的TypeDescriptionProvider 這是我的TypeDescriptionProviders的樣子

//type AClass Custom Provider
class AClassTypeProvider : TypeDescriptionProvider
{
    private static TypeDescriptionProvider defaultTypeProvider = TypeDescriptor.GetProvider(typeof(AClass));

    public AClassTypeProvider (): base(defaultTypeProvider)
    {

    }

    public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object instance)
    {
        ICustomTypeDescriptor defaultDescriptor = base.GetTypeDescriptor(objectType, instance);

        //returns a custom type descriptor based on a UserPropertyHostType enum value, and the default descriptor
        return new InfCustomTypeDescriptor(UserPropertyHostType.SiteRegion, defaultDescriptor);
    }
}


//type BClass Custom Provider
class BClassTypeProvider : TypeDescriptionProvider
{
    private static TypeDescriptionProvider defaultTypeProvider =   TypeDescriptor.GetProvider(typeof(BClass));

    public BClassTypeProvider (): base(defaultTypeProvider)
    {

    }

    public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object instance)
    {
        ICustomTypeDescriptor defaultDescriptor = base.GetTypeDescriptor(objectType, instance);

        //returns a custom type descriptor based on a UserPropertyHostType enum value, and the default descriptor
        return new InfCustomTypeDescriptor(UserPropertyHostType.Building, defaultDescriptor);
    }
}

因此,我的每個自定義TypeDescriptionProvide都通過向其傳遞特定類型的默認TypeDescriptionProvider來調用基本(TypeDescriptionProvider父)基本構造函數。

GetTypeDescriptor()方法調用base.GetTypeDescriptor()來獲取默認描述符,然后由我的自定義類型描述符用於添加自定義屬性。

有沒有辦法將這些組合成一個具有相同功能但不依賴於特定類型的通用自定義TypeDescriptionProvider 我可以跳過在構造函數中提供父TypeDescriptionProvider但稍后在GetTypeDescriptor()方法中設置它時,我具體知道正在查詢的對象類型是什么? 或者是否有其他方法獲取類型的默認描述符,然后調用base.GetTypeDescriptor(Type t,object ins)方法?

這個泛型類應該做你想要的:

class CustomTypeProvider<T> : TypeDescriptionProvider
{
    private static TypeDescriptionProvider defaultTypeProvider = TypeDescriptor.GetProvider(typeof(T));

    public CustomTypeProvider(): base(defaultTypeProvider)
    {

    }

    public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object instance)
    {
        ICustomTypeDescriptor defaultDescriptor = base.GetTypeDescriptor(objectType, instance);

        //returns a custom type descriptor based on a UserPropertyHostType enum value, and the default descriptor
        return new InfCustomTypeDescriptor(UserPropertyHostType.SiteRegion, defaultDescriptor);
    }
}

我過去通過使用泛型類型處理過這個問題:

public class MyTypeDescriptionProvider<T> : TypeDescriptionProvider
{
  public MyTypeDescriptionProvider()
    : base(TypeDescriptor.GetProvider(typeof(T)))
  {
  }
}

我很確定你可以通過將類型作為構造函數參數傳遞,在非泛型類型中處理它:

public class MyTypeDescriptionProvider : TypeDescriptionProvider
{
  public MyTypeDescriptionProvider(Type t)
    : base(TypeDescriptor.GetProvider(t))
  {
  }
}

如果您不需要在提供程序中使用該類型,則可能更好 - 但尚未對其進行測試。

然后,當使用此提供程序時,類按以下方式注冊:

TypeDescriptor.AddProvider(new MyTypeDescriptionProvider<ClassA>(), typeof(ClassA));
TypeDescriptor.AddProvider(new MyTypeDescriptionProvider<ClassB>(), typeof(ClassB));

等等

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM