Wie man alle statischen Eigenschaften und deren Werte einer Klasse mit Reflection erhält
Ich habe eine Klasse wie diese:
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";
}
Ich möchte eine Anweisung schreiben, die a zurückgibtDictionary<string, string>
das hat diese Werte:
Key Value
--------------------------------------------
"StateName" "State Name"
"StateCode" "State Code"
"AreaName" "Area Name"
"Dore" "Period"
"Year" "Year"
Ich habe diesen Code, um einen Eigenschaftswert zu erhalten:
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;
}
Wie kann ich alle Eigenschaften und ihre Werte erhalten?