为什么我无法通过List< Customer>作为接受List< object>的方法的参数? [英] Why can't I pass List<Customer> as a parameter to a method that accepts List<object>?
问题描述
以下代码给了我这个错误:
无法从
转换System.Collections.Generic.List'
到
'System.Collections.Generic.List'。
如何向编译器,客户确实从对象继承?或者它只是没有继承与泛型集合对象(发送 List< string>
获取相同的错误)。
using System.Collections.Generic;
使用System.Windows;
使用System.Windows.Documents;
命名空间TestControl3423
{
public partial class Window2:Window
{
public Window2()
{
InitializeComponent() ;
列表< Customer> customers = Customer.GetCustomers();
FillSmartGrid(客户);
//列表< CorporateCustomer> corporateCustomers = CorporateCustomer.GetCorporateCustomers();
// FillSmartGrid(corporateCustomers);
$ b public void FillSmartGrid(List< object> items)
{
//对物品进行反射并动态显示
}
}
公共类客户
{
公共字符串名字{get;组; }
public string LastName {get;组; }
public string Street {get;组; }
public string Location {get;组; }
public string ZipCode {get;组; }
public static List< Customer> GetCustomers()
{
列表< Customer> customers = new List< Customer>();
customers.Add(新客户{FirstName =Jim,LastName =Jones,ZipCode =23434});
customers.Add(新客户{FirstName =Joe,LastName =Adams,ZipCode =12312});
customers.Add(新客户{FirstName =Jake,LastName =Johnson,ZipCode =23111});
customers.Add(新客户{FirstName =Angie,LastName =Reckar,ZipCode =54343});
customers.Add(new Customer {FirstName =Jean,LastName =Anderson,ZipCode =16623});
回报客户;
.NET没有协方差和反差(还)。
B从A派生并不意味着 List< B>
从 List< A>
派生。它没有。它们是两种完全不同的类型。
.NET 4.0将获得有限的协方差和反方差。
The following code gives me this error:
Cannot convert from 'System.Collections.Generic.List' to 'System.Collections.Generic.List'.
How can I indicate to the compiler that Customer indeed inherits from object? Or does it just not do inheritance with generic collection objects (sending a List<string>
gets the same error).
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 { get; set; }
public string ZipCode { get; set; }
public static List<Customer> GetCustomers()
{
List<Customer> customers = new List<Customer>();
customers.Add(new Customer { FirstName = "Jim", LastName = "Jones", ZipCode = "23434" });
customers.Add(new Customer { FirstName = "Joe", LastName = "Adams", ZipCode = "12312" });
customers.Add(new Customer { FirstName = "Jake", LastName = "Johnson", ZipCode = "23111" });
customers.Add(new Customer { FirstName = "Angie", LastName = "Reckar", ZipCode = "54343" });
customers.Add(new Customer { FirstName = "Jean", LastName = "Anderson", ZipCode = "16623" });
return customers;
}
}
}
.NET does not have co-variance and contra-variance (yet).
That B derives from A doesn't imply that List<B>
derives from List<A>
. It doesn't. They are two totally different types.
.NET 4.0 will get limited co-variance and contra-variance.
这篇关于为什么我无法通过List< Customer>作为接受List< object>的方法的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!