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 havePropertyDescriptor
instances that actually talk to fields (instead of properties). You will also probably need to write your own bespkePropertyDescriptor
implementation sinceReflectPropertyDescriptor
isinternal
(you could perhaps use reflection to obtain that). Ultimately, you will have to use reflection for the implementation, but you can achieve the requirement thatTypeDescriptor.GetProperties(Type)
returnsPropertyDescriptor
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 implementingICustomTypeDescriptor
which is simpler than a fullTypeDescriptionProvider
.For an example of hooking a bespoke provider, see HyperDescriptor