我想從列表中初始化一個expandoObject。c#從列表中初始化expandoobject <T>
internal class CarKeyValue {
public CarKey CarKey { get; set; }
public string Value1 { get; set; }
public string Value2 { get; set; }
}
public enum CarKey {
Brand = 1,
Model = 2,
Year = 3,
FactoryLocation = 4,
//more than 400 key here...
}
var car = new List<CarKeyValue>{
new CarKeyValue {CarKey = CarKey.Brand, Value1 = "Ford"},
new CarKeyValue {CarKey = CarKey.Model, Value1 = "Focus",Value2 = "Titanium"},
new CarKeyValue {CarKey = CarKey.Year, Value1 = "1995"},
new CarKeyValue {CarKey = CarKey.FactoryLocation, Value1 = "Turkey",Value2="Bursa"},
};
dynamic expando = new ExpandoObject();
foreach(var item in car){
expando.[item.CarKey].Value1 = item.Value1;
//Incorrect primary expression.
expando.[item.CarKey].Value2 = item.Value2;
}
我該怎麼做?我需要使用Expando Object。我嘗試使用IDictionary<string,dynamic>
但引發了另一個例外。
有什麼可能的辦法嗎?
這就是我告訴你的。無論你是否使用匿名類,你仍然不能使用枚舉作爲鍵,你必須首先實例化它。 –
好吧,我知道,不能使用枚舉,但我們集中不同的東西,我需要一個解決方案,我發現它。可能是我用錯了你的方式,但它不適合我。如果你有一個工作.net小提琴的例子,我想看看它。 –