相关疑难解决方法(0)

为什么我不能将List <Customer>作为参数传递给接受List <object>的方法?

以下代码给出了这个错误:

无法从'System.Collections.Generic.List'转换为'System.Collections.Generic.List'.

如何向编译器指出Customer确实从对象继承?或者它只是不与泛型集合对象继承(发送List<string>获取相同的错误).

using System.Collections.Generic;
using System.Windows;
using System.Windows.Documents;

namespace TestControl3423
{
    public partial class Window2 : Window
    {
        public Window2()
        {
            InitializeComponent();

            List<Customer> customers = Customer.GetCustomers();
            FillSmartGrid(customers);

            //List<CorporateCustomer> corporateCustomers = CorporateCustomer.GetCorporateCustomers();
            //FillSmartGrid(corporateCustomers);
        }


        public void FillSmartGrid(List<object> items)
        {
            //do reflection on items and display dynamically
        }
    }

    public class Customer
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Street { get; set; }
        public string Location { …
Run Code Online (Sandbox Code Playgroud)

c# generics

12
推荐指数
4
解决办法
2万
查看次数

标签 统计

c# ×1

generics ×1