简体   繁体   English

无法从自定义对象转换为对象或动态

[英]Cannot convert from custom object to object or dynamic

I have a List which I would like to populate with different types of objects, trying to do it with object\\dynamic, but it doesn't, even when casting.我有一个列表,我想用不同类型的对象填充它,试图用 object\\dynamic 来做,但它没有,即使在投射时也是如此。 using asp.net core.使用asp.net核心。 See my code:看我的代码:

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;
}

Depending on what you are trying to do, I can see two solutions:根据您尝试执行的操作,我可以看到两种解决方案:

Create a list of TWO different dictionaries创建两个不同词典的列表

    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;
    }

Create one dictionary with all the values创建一个包含所有值的字典

    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;
    }

There are a couple of issues here, both related to variance.这里有几个问题,都与方差有关。

This won't work because List<T> , or any other class in .NET for that matter, does not support variance.这将不起作用,因为List<T>或 .NET 中的任何其他类不支持方差。

In other words T has to be a specific type, and does not respect inheritance / substitutability as with non-generic types.换句话说, T必须是特定类型,并且不像非泛型类型那样尊重继承/可替换性。

Similarly, for Dictionary<TKey, TValue> , TValue is not variant, so you can't simply use object as the value.同样,对于Dictionary<TKey, TValue>TValue不是变体,因此您不能简单地使用object作为值。

IEnumerable<out T> on the other hand is covariant so you could do this:另一方面, IEnumerable<out T>是协变的,所以你可以这样做:

public IEnumerable<IDictionary> getDifferentItems()
{
   yield return getEmployees();
   yield return getCustomers();
}

IDictionary is used, as it is the only common ancestor (other than object) to Dictionary<string, Employee> and Dictionary<string, Customer> . IDictionary ,因为它是Dictionary<string, Employee>Dictionary<string, Customer>的唯一共同祖先(对象除外)。

This may satisfy your requirements, but you don't make clear whay you are trying to achive with your getDifferentItems method.这可能会满足您的要求,但您没有明确说明您尝试使用getDifferentItems方法实现的getDifferentItems

More information on variance can be found here .可以在此处找到有关差异的更多信息。

I'd personally make an interface, such as IPerson that has all the properties of Employees and Customers, such as Name, Address, ID, etc.我会亲自制作一个接口,例如 IPerson,它具有员工和客户的所有属性,例如姓名、地址、ID 等。

Set up your Customer and employee classes to implement IPerson设置您的客户和员工类以实施 IPerson

Then use a IPerson in your dictionary and you can add to the objects to that.然后在您的字典中使用 IPerson,您可以将其添加到对象中。

Here's some code:这是一些代码:

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);
        }
    }

Here is another solution :这是另一个解决方案:

    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;}
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM