【发布时间】:2012-09-18 10:13:50
【问题描述】:
我有一个这样的课程:
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;
}
如何获取所有属性及其值?
【问题讨论】:
-
那些是静态字段,而不是静态属性。你两个都想要吗?还是只有字段?
标签: c# reflection