【发布时间】:2020-02-27 13:20:45
【问题描述】:
我有一个列表,我想用不同类型的对象填充它,尝试使用 object\dynamic 来完成它,但它没有,即使在投射时也是如此。 使用 asp.net 核心。 查看我的代码:
public Dictionary<string, Employee> getEmployees(); //This method returns a dictionary of string as a key and Employee as a value.
public Dictionary<string, customer>()> getCustomers(); //same principal
public List<Dictionary<string, object>> getDifferentItems()
{
List<Dictionary<string, object>> listOfItems = new List<Dictionary<string, object>>();
listOfItems.add(getEmployees()); //Error
listOfItems.add(getCustomers()); //Error
return listOfItems;
}
【问题讨论】:
-
new List<string, object>是错字吗? -
List<T>只接受一个类型参数——这里的List<string, object>到底是什么意思?你的意思是Dictionary<string, object>?要么...?注意:您不能将Dictionary<string, Employee>转换为Dictionary<string, object>- 这不是“方差”的工作原理 -
类型错误——抱歉,这是我写的伪代码。尽快修复它
-
@Johnathan Barclay- 我修复了代码
-
@Marc Gravell - 我修复了代码
标签: c# dictionary object asp.net-core dynamicobject