I would like to get private properties of the class using TypeDescriptor in c#.
So far calling
TypeDescriptor.GetProperties(myType);
returns only public, non-static properties.
I have not found a way how to influence GetProperties or GetProvider methods to force them to return other than "default" (public, non-static) members.
Please do not suggest reflection (I am well aware of BindingFlags) unless it gives me a PropertyDescriptor object.
To do that you would have to write and register a custom TypeDescriptionProvider
that does use reflection. You can certainly, however, do this - you can even have PropertyDescriptor
instances that actually talk to fields (instead of properties). You will also probably need to write your own bespke PropertyDescriptor
implementation since ReflectPropertyDescriptor
is internal
(you could perhaps use reflection to obtain that). Ultimately, you will have to use reflection for the implementation, but you can achieve the requirement that TypeDescriptor.GetProperties(Type)
returns PropertyDescriptor
instances that you want.
You can do this for types outside your control, too. It should be stressed, however, that your intent is unusual.
If you were using the .GetProperties(instance)
overload, then you can also do this by implementing ICustomTypeDescriptor
which is simpler than a full TypeDescriptionProvider
.
For an example of hooking a bespoke provider, see HyperDescriptor