C# 无法从自定义对象转换为对象或动态对象
我有一个列表,我想用不同类型的对象填充它,尝试使用object\dynamic进行填充,但它没有,即使在强制转换时也是如此。 使用asp.net内核。 请参阅我的代码:C# 无法从自定义对象转换为对象或动态对象,c#,dictionary,object,asp.net-core,dynamicobject,C#,Dictionary,Object,Asp.net Core,Dynamicobject,我有一个列表,我想用不同类型的对象填充它,尝试使用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,
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;
}
公共字典getEmployees()//此方法返回字符串字典作为键,雇员字典作为值。
公共字典()>getCustomers()//同一负责人
公共列表getDifferentItems()
{
List listOfItems=新列表();
添加(getEmployees());//错误
添加(getCustomers());//错误
返回项目列表;
}
这里有几个问题,都与差异有关
这不起作用,因为List
,或者.NET中的任何其他类都不支持差异
换句话说,T
必须是一个特定的类型,并且与非泛型类型一样不考虑继承/替换性
类似地,对于字典
,TValue
不是变量,因此不能简单地使用对象
作为值
另一方面,IEnumerable
是协变的,因此可以执行以下操作:
public IEnumerable<IDictionary> getDifferentItems()
{
yield return getEmployees();
yield return getCustomers();
}
public IEnumerable getDifferentItems()
{
收益率回报率为();
收益率返回getCustomers();
}
使用IDictionary
,因为它是Dictionary
和Dictionary
的唯一共同祖先(对象除外)
这可能满足您的需求,但您没有明确说明使用getDifferentItems
方法想要实现什么
可以找到更多关于差异的信息。我会亲自制作一个界面,例如具有员工和客户所有属性的IPerson,例如姓名、地址、ID等 设置您的客户和员工类以实现IPerson 然后在你的字典中使用一个IPerson,你可以把对象添加到其中 下面是一些代码:
public class Employee : IPerson
{
public int ID { get; set; }
public string Name { get; set; }
}
public class Customer : IPerson
{
public int ID { get; set; }
public string Name { get; set; }
}
public interface IPerson
{
int ID { get; set; }
string Name { get; set; }
}
public class Test
{
public void MyTest()
{
List<Dictionary<string, IPerson>> listOfItems = new List<Dictionary<string, IPerson>>();
Dictionary<string, IPerson> myEmployees = new Dictionary<string, IPerson>();
string someString = "blah";
Employee e = new Employee();
e.Name = "Bob";
e.ID = 1;
myEmployees.Add(someString, e);
Dictionary<string, IPerson> myCustomers = new Dictionary<string, IPerson>();
string someOtherString = "blah";
Customer c = new Customer();
c.Name = "Robert";
c.ID = 2;
myCustomers.Add(someOtherString, c);
listOfItems.Add(myEmployees);
listOfItems.Add(myCustomers);
}
}
public类员工:IPerson
{
公共int ID{get;set;}
公共字符串名称{get;set;}
}
公共类客户:IPerson
{
公共int ID{get;set;}
公共字符串名称{get;set;}
}
公共接口IPerson
{
int ID{get;set;}
字符串名称{get;set;}
}
公开课考试
{
公共无效MyTest()
{
List listOfItems=新列表();
Dictionary myEmployees=新字典();
string someString=“blah”;
员工e=新员工();
e、 Name=“Bob”;
e、 ID=1;
添加(someString,e);
Dictionary myCustomers=新字典();
string someOtherString=“blah”;
客户c=新客户();
c、 Name=“罗伯特”;
c、 ID=2;
添加(someOtherString,c);
添加(myEmployees)列表项;
添加(我的客户);
}
}
根据您的尝试,我可以看到两种解决方案:
创建两个不同词典的列表
公共字典getEmployees(){
返回新字典();
}
公共字典getCustomers(){
返回新字典();
}
公共列表getDifferentItems()
{
List listOfItems=新列表();
添加(this.getEmployees().ToDictionary(entry=>(string)entry.Key,
entry=>(object)entry.Value);
添加(this.getCustomers().ToDictionary(entry=>(string)entry.Key,
entry=>(object)entry.Value);
返回项目列表;
}
创建一个包含所有值的字典
公共字典getEmployees(){
返回新字典();
}
公共字典getCustomers(){
返回新字典();
}
公共字典getDifferentItems()
{
字典列表项=新字典();
foreach(getEmployees()中的var条目){
添加(entry.Key,entry.Value);
}
foreach(getCustomers()中的var条目){
添加(entry.Key,entry.Value);
}
返回项目列表;
}
这里是另一个解决方案:
public class Test
{
Dictionary<string, object> listOfItems = new Dictionary<string, object>();
List<Employee> employees = new List<Employee>();
List<customer> customers = new List<customer>();
public Dictionary<string, object> getEmployees()
{
return employees.GroupBy(x => x.name, y => (object)y).ToDictionary(x => x.Key, y => y.FirstOrDefault());
}//This method returns a dictionary of string as a key and Employee as a value.
public Dictionary<string, object> getCustomers()
{
return customers.GroupBy(x => x.name, y => (object)y).ToDictionary(x => x.Key, y => y.FirstOrDefault());
} //same principal
public Dictionary<string, object> getDifferentItems()
{
listOfItems = getEmployees();
listOfItems.Concat(getCustomers());
return listOfItems;
}
}
public class Employee
{
public string name { get;set;}
}
public class customer
{
public string name { get;set;}
}
公共类测试
{
字典列表项=新字典();
列出员工=新列表();
列出客户=新列表();
公共字典getEmployees()
{
return employees.GroupBy(x=>x.name,y=>(object)y).ToDictionary(x=>x.Key,y=>y.FirstOrDefault());
}//此方法返回字符串字典作为键,雇员字典作为值。
公共字典getCustomers()
{
return customers.GroupBy(x=>x.name,y=>(object)y).ToDictionary(x=>x.Key,y=>y.FirstOrDefault());
}//相同的主体
公共字典getDifferentItems()
{
listOfItems=getEmployees();
Concat(getCustomers());
返回项目列表;
}
}
公营雇员
{
公共字符串名称{get;set;}
}
公共类客户
{
公共字符串名称{get;set;}
}
新列表是否是打字错误?列表
只接受一个类型参数-您这里所说的列表
到底是什么意思?你的意思是把它写成字典吗?或注意:您不能将字典
强制转换为字典
-这不是“variance”类型错误的工作方式-抱歉,这是我编写的伪代码。修好它soon@Johnathan巴克莱-我修好了code@MarcGravell-我修复了代码谢谢-它基本上用于rest方法-以获取静态查找表的集合。在我的代码中,容器(列表)被一个字典替换,以获取表名的标识符作为键,其内容(键、值)作为值。这是我最初的想法,但类型已经存在,非常不同,没有simi
public Dictionary<string, Employee> getEmployees() {
return new Dictionary<string, Employee>();
}
public Dictionary<string, Customer> getCustomers() {
return new Dictionary<string, Customer>();
}
public Dictionary<string, object> getDifferentItems()
{
Dictionary<string, object> listOfItems = new Dictionary<string, object>();
foreach (var entry in getEmployees()) {
listOfItems.Add(entry.Key, entry.Value);
}
foreach (var entry in getCustomers()) {
listOfItems.Add(entry.Key, entry.Value);
}
return listOfItems;
}
public class Test
{
Dictionary<string, object> listOfItems = new Dictionary<string, object>();
List<Employee> employees = new List<Employee>();
List<customer> customers = new List<customer>();
public Dictionary<string, object> getEmployees()
{
return employees.GroupBy(x => x.name, y => (object)y).ToDictionary(x => x.Key, y => y.FirstOrDefault());
}//This method returns a dictionary of string as a key and Employee as a value.
public Dictionary<string, object> getCustomers()
{
return customers.GroupBy(x => x.name, y => (object)y).ToDictionary(x => x.Key, y => y.FirstOrDefault());
} //same principal
public Dictionary<string, object> getDifferentItems()
{
listOfItems = getEmployees();
listOfItems.Concat(getCustomers());
return listOfItems;
}
}
public class Employee
{
public string name { get;set;}
}
public class customer
{
public string name { get;set;}
}