我意识到这是一个不寻常的问题,但是:
我创建了一个自定义TypeDescriptionProvider,它可以TypeDescriptors根据请求的对象类型存储和返回不同的.然而,我注意到无论TypeDescriptionProvider与Type(be-it custom或default)相关联,TypeDescriptor.GetProvider()总是返回一个(内部类)System.ComponentModel.TypeDescriptor.TypeDescriptionNode对象(实际的某种包装器TypeDescriptionProvider).反过来调用GetTypeDescriptor()这个对象总是返回一个System.ComponentModel.TypeDescriptor.TypeDescriptionNode.DefaultTypeDescriptor对象(另一个包装器),而陌生人仍然没有调用TypeDescriptionProvider实际的GetTypeDescriptor()方法.
这是否意味着无法从提供者那里找回对象的实际内容TypeDescriptionProvider或其实际内容TypeDescriptor?返回类名,属性等的方法仍然按预期工作DefaultTypeDescriptor,但我无法比较或找出两个对象是否使用相同的TypeDescriptor(这是我目前需要的).
有谁知道如何获得实际的TypeDescriptionProvider,或从包装的提供者获得实际的TypeDescriptor?
先感谢您.
例如:
public class TestTypeDescriptionProvider : TypeDescriptionProvider
{
private static Dictionary<Type, ICustomTypeDescriptor> _descriptors = new Dictionary<Type, ICustomTypeDescriptor>();
...static method to add to the cache...
public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object instance)
{
if (objectType == null)
return _descriptors[instance.GetType()];
return _descriptors[objectType];
}
}
...
TypeDescriptionProvider p = TypeDescriptor.GetProvider(obj.GetType()); //p is a TypeDescriptionNode
ICustomTypeDescriptor td = p.GetTypeDescriptor(obj); // td is a DefaultTypeDescriptor
Run Code Online (Sandbox Code Playgroud)
TypeDescriptor.GetProvider如果您使用 .NET Reflector 等工具查看实现,您会发现返回的类型是相当硬编码的。就像您观察到的那样,它总是返回 a TypeDescriptionNode。同样的故事GetTypeDescriptor。
现在,您可以做的是使用反射机制来获取实际TypeDescriptionProvider的TypeDescriptionNode. 这很简单,只需获取名为 的私有字段Provider,这是 TypeDescriptionNode 存储实际实现的地方。当然不支持,但我认为这不会在不久的将来改变,而且我真的没有看到更好的方法......
| 归档时间: |
|
| 查看次数: |
2851 次 |
| 最近记录: |