C#从子类获取详细信息

Haf*_*112 2 c# generics inheritance subclass

我是C#的完全新手,请原谅我,如果这看起来很奇怪.

我有一个名为vefHlutir的抽象类

namespace Klasasafn
{
    public abstract class vefHlutur
    {
        public abstract List<String> columnNames();
        public abstract List<String> toStringList();
    }
}
Run Code Online (Sandbox Code Playgroud)

//Here is an object that inherits from this abstract class:

namespace Klasasafn
{
    [Table(Name="Users")]
    public class User: vefHlutur
    {
        public override List<String> columnNames()
        {
            List<String> p = new List<String>();
            p.Add("Nafn");
            p.Add("Email");
            p.Add("Lýsing");
            return p;
        }
        public override List<String> toStringList()
        {
            List<String> p = new List<String>();
            p.Add(name);
            p.Add(email);
            p.Add(descr);
            return p;
        }
    ... more stuff here
    }

} 
Run Code Online (Sandbox Code Playgroud)

//And here is the code I'm trying to run, Item, User and Category all inherit from vefHlutir:

List<Klasasafn.Item> hlutir;
List<Klasasafn.User> notendur;
List<Klasasafn.Category> flokkar;
void Page_Init(object sender, EventArgs e)
{
    hlutir = Fac.getItemList();
    notendur = Fac.getUserList();
    flokkar = Fac.getCategoryList();

    prenta(notendur, Table1);
}

protected void Page_Load(object sender, EventArgs e)
{

}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
    if (DropDownList1.SelectedIndex == 0)
    {
        prenta(notendur, Table1);
    }
    else if (DropDownList1.SelectedIndex == 1)
    {
        prenta(hlutir, Table1);
    }
    else
        prenta(flokkar, Table1);
}
private void prenta(List<vefHlutur> foo, Table f)
{
    List<String> columnNames = foo[0].columnNames();
    TableRow tRow1 = new TableRow();
    f.Rows.Add(tRow1);
    foreach (String i in columnNames)
    {
        TableCell columnNameCell = new TableCell();
        tRow1.Cells.Add(columnNameCell);
        columnNameCell.Controls.Add(new LiteralControl(i));
    }
    foreach (vefHlutur j in foo)
    {
        TableRow tRow = new TableRow();
        f.Rows.Add(tRow);
        List<String> töfluHlutir = j.toStringList();
        foreach (String k in töfluHlutir)
        {
            TableCell tCell1 = new TableCell();
            tRow.Cells.Add(tCell1);
            tCell1.Controls.Add(new LiteralControl(k));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我的问题是我不能使用prenta方法.

我总是得到这些错误:

错误1'Forsíða.prenta(System.Collections.Generic.List,System.Web.UI.WebControls.Table)'的最佳重载方法匹配有一些无效的参数

错误2参数'1':无法从'System.Collections.Generic.List'转换为'System.Collections.Generic.List

我该如何解决这个问题?

Jar*_*Par 9

问题是在C#中,键入List<ChildClass>方法时无法使用该类型List<ParentClass>.这种类型的转换称为协方差,它在C#中直到4.0才可用,然后仅在接口和事件上可用.

你可以做的是使方法通用并添加约束.

private void prenta<T>(List<T> foo, Table f)
  where T : vefHlutur
{
  ...
}
Run Code Online (Sandbox Code Playgroud)

这段代码正在做的是说prenta将接受a List<T>作为任何T为或来自vefHlutur类型的派生的情况的第一个参数.它还允许您将类型T视为关于调用方法,属性等的类型vefHlutur ...这应该允许您的方案工作.