无法从自定义对象转换为对象或动态
Cannot convert from custom object to object or dynamic
我有一个列表,我想用不同类型的对象填充它,试图用 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;
}
这里有几个问题,都与方差有关。
这行不通,因为 List<T>
或 .NET 中的任何其他 class 不支持差异。
换句话说,T
必须是特定类型,并且不像非泛型类型那样尊重继承/可替换性。
同样,对于Dictionary<TKey, TValue>
,TValue
不是变体,所以不能简单地使用object
作为值。
另一方面,IEnumerable<out T>
是协变的,因此您可以这样做:
public IEnumerable<IDictionary> getDifferentItems()
{
yield return getEmployees();
yield return getCustomers();
}
使用 IDictionary
,因为它是 Dictionary<string, Employee>
和 Dictionary<string, Customer>
.
的唯一共同祖先(对象除外)
这可能会满足您的要求,但您没有明确说明您要使用 getDifferentItems
方法实现的目标。
可以找到有关方差的更多信息here。
我会亲自制作一个界面,例如 IPerson,它具有 Employees 和 Customers 的所有属性,例如 Name、Address、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 Dictionary<string, Employee> getEmployees() {
return new Dictionary<string, Employee>();
}
public Dictionary<string, Customer> getCustomers() {
return new Dictionary<string, Customer>();
}
public List<Dictionary<string, object>> getDifferentItems()
{
List<Dictionary<string, object>> listOfItems = new List<Dictionary<string, object>>();
listOfItems.Add(this.getEmployees().ToDictionary(entry => (string)entry.Key,
entry => (object)entry.Value));
listOfItems.Add(this.getCustomers().ToDictionary(entry => (string)entry.Key,
entry => (object)entry.Value));
return listOfItems;
}
创建一个包含所有值的字典
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;}
}
我有一个列表,我想用不同类型的对象填充它,试图用 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;
}
这里有几个问题,都与方差有关。
这行不通,因为 List<T>
或 .NET 中的任何其他 class 不支持差异。
换句话说,T
必须是特定类型,并且不像非泛型类型那样尊重继承/可替换性。
同样,对于Dictionary<TKey, TValue>
,TValue
不是变体,所以不能简单地使用object
作为值。
IEnumerable<out T>
是协变的,因此您可以这样做:
public IEnumerable<IDictionary> getDifferentItems()
{
yield return getEmployees();
yield return getCustomers();
}
使用 IDictionary
,因为它是 Dictionary<string, Employee>
和 Dictionary<string, Customer>
.
这可能会满足您的要求,但您没有明确说明您要使用 getDifferentItems
方法实现的目标。
可以找到有关方差的更多信息here。
我会亲自制作一个界面,例如 IPerson,它具有 Employees 和 Customers 的所有属性,例如 Name、Address、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 Dictionary<string, Employee> getEmployees() {
return new Dictionary<string, Employee>();
}
public Dictionary<string, Customer> getCustomers() {
return new Dictionary<string, Customer>();
}
public List<Dictionary<string, object>> getDifferentItems()
{
List<Dictionary<string, object>> listOfItems = new List<Dictionary<string, object>>();
listOfItems.Add(this.getEmployees().ToDictionary(entry => (string)entry.Key,
entry => (object)entry.Value));
listOfItems.Add(this.getCustomers().ToDictionary(entry => (string)entry.Key,
entry => (object)entry.Value));
return listOfItems;
}
创建一个包含所有值的字典
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;}
}