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 關注
- 139 瀏覽
添加回答
舉報