2012-07-09 179 views

回答

3

您可以使用Reflection來讀取屬性名稱和值。例如,要得到一個類型的公共屬性的列表,你可以使用GetProperties方法:

var properties = typeof(Setting); 
foreach (var prop in properties) 
{ 
    // here you can access the name of the property using prop.Name 
    // if you want to access the value you could use the prop.GetValue method 
} 
1

您可以使用反射來獲取你的類的屬性,你可以像一些事情做到這一點。

PropertyInfo[] propertyInfos; 
propertyInfos = typeof(MyClass).GetProperties(BindingFlags.Public | 
               BindingFlags.Static); 


foreach (PropertyInfo propertyInfo in propertyInfos) 
{ 
    if (propertyInfo.Name == yourString) 
    { 
     return yourString; 
    } 
}