C # Getting the actual TypeDescriptionProvider or TypeDescriptor object

I understand that this is an unusual problem, but:

I created a custom TypeDescriptionProvider that can store and return different TypeDescriptors based on the requested object type. However, I noticed that regardless of the TypeDescriptionProvider associated with the type (be-it custom or default), TypeDescriptor.GetProvider() always returns an object (inner class) System.ComponentModel.TypeDescriptor.TypeDescriptionNode (some wrapper around the actual TypeDescriptionProvider ). In turn, calling GetTypeDescriptor() on this object always returns a System.ComponentModel.TypeDescriptor.TypeDescriptionNode.DefaultTypeDescriptor object (another shell), and the stranger still does not call the TypeDescriptionProvider method TypeDescriptionProvider actual GetTypeDescriptor() .

Does this mean that there really is no way to return the actual TypeDescriptionProvider or its TypeDescriptor from its provider? Methods for returning class name, properties, etc. They still work as expected on the DefaultTypeDescriptor , but I cannot compare or find out if the two objects are using the same TypeDescriptor (which I currently need).

Does anyone know how to get the actual TypeDescriptionProvider , or get the actual TypeDescriptor from a wrapped provider?

Thanks in advance.

Example:

 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 
+6
source share
2 answers

If you look at the implementation of TypeDescriptor.GetProvider with a tool like .NET Reflector, you will see that the return type is pretty hardcoded. It always returns TypeDescriptionNode , as you noticed. Same story for GetTypeDescriptor .

Now you can use Reflection mechanisms to get the actual TypeDescriptionProvider from TypeDescriptionNode . It's quite simple, just get a private field called Provider , here TypeDescriptionNode stores the actual implementation. Of course, this is not supported, but I do not think that this will change in the near future, and I really do not see a better way ...

+4
source

You can check if both types have the same TypeDescriptionProviderAttribute .

0
source

Source: https://habr.com/ru/post/897714/


All Articles