ICustomTypeDescriptor GetConverter 实现
ICustomTypeDescriptor GetConverter implementation
我有一个用 TypeDescriptionProviderAttribute
装饰的基础 class,它指向 ICustomTypeDescriptor
的自定义实现。
有一个派生的 class 用 TypeConverterAttribute
修饰来进行自定义类型转换。
BaseClassTypeDescriptor
通过调用静态TypeDescriptor.GetConverter
方法实现ICustomTypeDescriptor.GetConverter
。该方法有两个参数:所讨论的类型(我有一个引用),以及一个指示是否允许调用自定义行为的标志。这必须设置为 true
以防止无限循环。
代码的精简版本如下所示:
[TypeDescriptionProvider(typeof(BaseClassTypeDescriptionProvider))]
public class BaseClass
{
public class BaseClassTypeDescriptionProvider : TypeDescriptionProvider
{
public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType_, object instance_)
{
return new BaseClassTypeDescriptor(objectType_);
}
}
public class BaseClassTypeDescriptor : ICustomTypeDescriptor
{
private Type _type;
public BaseClassTypeDescriptor(Type type_)
{
_type = type_;
}
TypeConverter ICustomTypeDescriptor.GetConverter()
{
return TypeDescriptor.GetConverter(_type, true);
}
}
}
[TypeConverter(typeof(MyTypeConverter))]
public class DerivedClass : BaseClass
{
}
问题是这个标志似乎不仅绕过了 BaseClassTypeDescriptor
,而且似乎还阻止 .NET 识别派生的 class.[=23= 上的 TypeConverterAttribute
]
我通过在 MyCustomTypeConverter.GetConverter
的实现中重新实现对 TypeConverterAttribute
的检查来解决这个问题,就像这样:
TypeConverter ICustomTypeDescriptor.GetConverter()
{
object[] typeConverterAttributeArray = _type.GetCustomAttributes(typeof(TypeConverterAttribute), true);
if (typeConverterAttributeArray.Length == 1)
{
TypeConverterAttribute a = (TypeConverterAttribute)typeConverterAttributeArray[0];
Type converterType = MyTypeLoader.GetType(a.ConverterTypeName);
return (TypeConverter)Activator.CreateInstance(converterType);
}
else
{
return TypeDescriptor.GetConverter(_type, true);
}
}
这远非理想的解决方案。关于我如何将责任放回原处有什么建议吗?
在您的示例中,TypeDescriptor.GetConverter(object component, bool noCustomTypeDesc)
的重载似乎 return Type
类型的 TypeDescriptor
,因为它需要一个实例。
我用
试过你的例子
return TypeDescriptor.GetConverter(Activator.CreateInstance(_type), true);
并得到了 bool
值本应阻止的无限循环。
我不知道为什么会这样,也不知道是否以某种方式记录了这一点,但我敢打赌他们根本不希望 TypeDescriptionProvider
调用 TypeDescriptor.GetConverter()
。
我有一个用 TypeDescriptionProviderAttribute
装饰的基础 class,它指向 ICustomTypeDescriptor
的自定义实现。
有一个派生的 class 用 TypeConverterAttribute
修饰来进行自定义类型转换。
BaseClassTypeDescriptor
通过调用静态TypeDescriptor.GetConverter
方法实现ICustomTypeDescriptor.GetConverter
。该方法有两个参数:所讨论的类型(我有一个引用),以及一个指示是否允许调用自定义行为的标志。这必须设置为 true
以防止无限循环。
代码的精简版本如下所示:
[TypeDescriptionProvider(typeof(BaseClassTypeDescriptionProvider))]
public class BaseClass
{
public class BaseClassTypeDescriptionProvider : TypeDescriptionProvider
{
public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType_, object instance_)
{
return new BaseClassTypeDescriptor(objectType_);
}
}
public class BaseClassTypeDescriptor : ICustomTypeDescriptor
{
private Type _type;
public BaseClassTypeDescriptor(Type type_)
{
_type = type_;
}
TypeConverter ICustomTypeDescriptor.GetConverter()
{
return TypeDescriptor.GetConverter(_type, true);
}
}
}
[TypeConverter(typeof(MyTypeConverter))]
public class DerivedClass : BaseClass
{
}
问题是这个标志似乎不仅绕过了 BaseClassTypeDescriptor
,而且似乎还阻止 .NET 识别派生的 class.[=23= 上的 TypeConverterAttribute
]
我通过在 MyCustomTypeConverter.GetConverter
的实现中重新实现对 TypeConverterAttribute
的检查来解决这个问题,就像这样:
TypeConverter ICustomTypeDescriptor.GetConverter()
{
object[] typeConverterAttributeArray = _type.GetCustomAttributes(typeof(TypeConverterAttribute), true);
if (typeConverterAttributeArray.Length == 1)
{
TypeConverterAttribute a = (TypeConverterAttribute)typeConverterAttributeArray[0];
Type converterType = MyTypeLoader.GetType(a.ConverterTypeName);
return (TypeConverter)Activator.CreateInstance(converterType);
}
else
{
return TypeDescriptor.GetConverter(_type, true);
}
}
这远非理想的解决方案。关于我如何将责任放回原处有什么建议吗?
在您的示例中,TypeDescriptor.GetConverter(object component, bool noCustomTypeDesc)
的重载似乎 return Type
类型的 TypeDescriptor
,因为它需要一个实例。
我用
试过你的例子return TypeDescriptor.GetConverter(Activator.CreateInstance(_type), true);
并得到了 bool
值本应阻止的无限循环。
我不知道为什么会这样,也不知道是否以某种方式记录了这一点,但我敢打赌他们根本不希望 TypeDescriptionProvider
调用 TypeDescriptor.GetConverter()
。