Các mã sau đây mang lại cho tôi lỗi này:Tại sao tôi không thể chuyển Danh sách <Customer> làm tham số cho phương thức chấp nhận Danh sách <object>?
Cannot convert from 'System.Collections.Generic.List' to 'System.Collections.Generic.List'.
Làm thế nào tôi có thể chỉ ra để trình biên dịch mà khách hàng thực sự được thừa hưởng từ đối tượng? Hoặc thực hiện nó không làm thừa kế với các đối tượng thu chung chung (gửi List<string>
bị lỗi tương tự).
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;
}
}
}
Tại sao bạn lại tham gia Danh sách
để thực hiện phản chiếu trên chúng và xử lý chúng động –
Bạn vẫn có thể thực hiện phản chiếu trên đối tượng 'Customer' giống như bạn có thể trên một' đối tượng'. –