Tutorial :Windows control returns different values when accessed different ways


I have a control, hosted on DesignSurface.

When its Location property is accessed by control.Location, and when it's accessed by propertyDescriptor.GetValue(control), I get different values.

propertyDescriptor is of type PropertyDescriptor.

Does anyone have a solution to this? I have checked that the object instance is the same.


Well it might depend on the object. and how you got your type descriptor. E.g. it could be a custom descriptor which could return what ever. If that's the case you can give the GetTypedescriptor method information of not to use custom typedescriptors. (sry for not posting the actual code but I don't have an IDE available and can't remember the exact syntax).

A different approach would be to ue a PropertyInfo instead of a propertyDescriptor (if the rest of the code works with a System.Reflection.PropertyInfo).

You can get the PropertyInfo of the Property Length for the stype string like this

typeof(string).GetProperty("Length"); or if it's an type unknown at compile time like this: obj.GetType().GetProperty("Length");

if you need to loop through all properties call GetProperties instead.

But all that PropertyInfo relies on my guess that you'd be able to use PropertyInfo instead of PropertyDescriptor

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »