1 回答
TA贡献1828条经验 获得超3个赞
您需要从每个字段获取值并将其转换为适当的类型。然后就可以访问内部属性了
Data actualDataObject = // whereever you get it from
Dictionary<string, string> ChampIdDict = new Dictionary<string, string>();
FieldInfo[] fields = typeof(Data).GetFields();
foreach (var field in fields)
{
Champions temp = (Champions)field.GetValue(actualDataObject);
string key = temp.key;
string name = temp.name;
ChampIdDict.Add(key, name)
}
在 linq 中,这可能看起来像这样:
Dictionary<string, string> ChampIdDict = (from field in fields
let temp = (Champions)field.GetValue(actualDataObject)
select new {key = temp.key, name = temp.name})
.ToDictionary(x => x.key, x => x.name);
- 1 回答
- 0 关注
- 102 浏览
添加回答
举报