Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Consider the following code.

Object obj;
PropertyDescriptorCollection A = TypeDescriptor.GetProperties(obj);
PropertyInfo[] B = obj.GetType().GetProperties();

I'm trying to understand the difference between A and B. From what I understand TypeDescriptor.GetProperties() will return custom TypeDescriptor properties, where as Type.GetProperties() will only return intrinsic "real" properties of the object. Is this right? If obj doesn't have any custom TypeDescriptor properties then it just defaults to also returning the literal intrinsic properties of the object.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
769 views
Welcome To Ask or Share your Answers For Others

1 Answer

obj.GetType().GetProperties() does not return a PropertyDescriptorCollection, it returns a System.Reflection.PropertyInfo[]. The PropertyInfo class does, as you suggest, represent only actual properties created on the object. A PropertyDescriptor is either a custom concrete child of the PropertyDescriptor class (implemented by the type defining the custom descriptor), or is an instance of the sealed internal class ReflectPropertyDescriptor that uses the PropertyInfo class to provide dynamic invocation of the property.

So for a class that does not define a custom descriptor, you will functionally get the same objects back, though the PropertyDescriptor is abstracting away the PropertyInfo.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...