22
一類的價值觀我哈瓦一類是這樣的:如何讓所有的靜態屬性和它的使用反射
public class tbl050701_1391_Fields
{
public static readonly string StateName = "State Name";
public static readonly string StateCode = "State Code";
public static readonly string AreaName = "Area Name";
public static readonly string AreaCode = "Area Code";
public static readonly string Dore = "Period";
public static readonly string Year = "Year";
}
我想寫一個返回Dictionary<string, string>
具有這些價值的一些語句:
Key Value
--------------------------------------------
"StateName" "State Name"
"StateCode" "State Code"
"AreaName" "Area Name"
"Dore" "Period"
"Year" "Year"
我有獲得一個屬性值驗證碼:
public static string GetValueUsingReflection(object obj, string propertyName)
{
var field = obj.GetType().GetField(propertyName, BindingFlags.Public | BindingFlags.Static);
var fieldValue = field != null ? (string)field.GetValue(null) : string.Empty;
return fieldValue;
}
如何,我可以得到所有的屬性和它們的值?
那些是靜態字段,而不是靜態屬性。你想要兩個嗎?還是隻有田野? – CodesInChaos