我有一个只包含字符串属性的静态类。我想将该类转换为具有key=PropName
,的名称-值对字典value=PropValue
。
下面是我写的代码:
void Main()
{
Dictionary<string, string> items = new Dictionary<string, string>();
var type = typeof(Colors);
var properties = type.GetProperties(BindingFlags.Static);
/*Log properties found*/
/*Iam getting zero*/
Console.WriteLine("properties found: " +properties.Count());
foreach (var item in properties)
{
string name = item.Name;
string colorCode = item.GetValue(null, null).ToString();
items.Add(name, colorCode);
}
/*Log items created*/
Console.WriteLine("Items in dictionary: "+items.Count());
}
public static class Colors
{
public static string Gray1 = "#eeeeee";
public static string Blue = "#0000ff";
}
输出
properties found: 0
Items in dictionary: 0
它没有读取任何属性 - 谁能告诉我我的代码有什么问题?