Tôi HAVA một lớp như thế này:Làm thế nào để có được tất cả các thuộc tính tĩnh và các giá trị của một lớp học sử dụng phản ánh
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";
}
Tôi muốn viết một số tuyên bố rằng trả về một Dictionary<string, string>
rằng có những giá trị này:
Key Value
--------------------------------------------
"StateName" "State Name"
"StateCode" "State Code"
"AreaName" "Area Name"
"Dore" "Period"
"Year" "Year"
tôi có mã này để nhận giá trị một tài sản:
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;
}
Làm thế nào tôi có thể nhận được tất cả các thuộc tính và giá trị của chúng?
Đó là các trường tĩnh, không phải thuộc tính tĩnh. Bạn có muốn cả hai? Hoặc chỉ các trường? – CodesInChaos