2010-10-31 78 views

回答

4

另一种方法是:

 PropertyInfo info = obj.GetType().GetProperty("PropertyNameToFind"); 

     if (info != null) 
     { 
      // Property exists in this type... 
     } 
1

给看看System.Reflection.PropertyInfo类。

下面是使用反射示例用法

using System.Reflection; // reflection namespace 

// get all public static properties of MyClass type 
PropertyInfo[] propertyInfos; 
propertyInfos = typeof(MyClass).GetProperties(BindingFlags.Public |            BindingFlags.Static); 
// sort properties by name 
Array.Sort(propertyInfos, delegate(PropertyInfo propertyInfo1, PropertyInfo propertyInfo2) 
     { return propertyInfo1.Name.CompareTo(propertyInfo2.Name); }); 

// write property names 
foreach (PropertyInfo propertyInfo in propertyInfos) { 
    Console.WriteLine(propertyInfo.Name); 
}