TypeDescriptor.GetProperties() vs Type.GetProperties()

前端 未结 3 937
自闭症患者
自闭症患者 2020-12-05 22:39

Consider the following code.

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


        
3条回答
  •  暖寄归人
    2020-12-05 23:36

    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.

提交回复
热议问题