C# 从子类中获取详细信息

标签 c# generics inheritance subclass

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

我有一个名为vefHlutir的抽象类

<pre><code>namespace Klasasafn { public abstract class vefHlutur { public abstract List<String> columnNames(); public abstract List<String> toStringList(); } } </code></pre> <p>//Here is an object that inherits from this abstract class:</p> <pre><code>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 } } </code></pre> <p>//And here is the code I'm trying to run, Item, User and Category all inherit from vefHlutir:</p> <pre><code>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)); } } } </code></pre>

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

我总是遇到这些错误:

错误 1 ​​The best overloaded method match for 'Forsíða.prenta(System.Collections.Generic.List, System.Web.UI.WebControls.Table)' has some invalid arguments

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

我该如何解决?

最佳答案

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

不过,您可以做的是使方法通用并添加约束。

private void prenta<T>(List<T> foo, Table f)
  where T : vefHlutur
{
  ...
}

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

关于C# 从子类中获取详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/990808/

相关文章:

c# - 如何判断一个类型是否在继承层次中

java - Java中的虚拟表和抽象

c# - Newtonsoft Json.NET ReferenceLoopHandling 和 JavascriptDateTimeConverter

c# - 在 Roslyn 中获取 SymbolCallerInfo 的通用参数

objective-c - 有没有办法向协议(protocol)添加泛型类型参数?

带有通配符的 Java 泛型类型

java - Java中使用接口(interface)的匿名内部类

c# - 防止 GC 在 C# 中获取我的委托(delegate)

c# - 关闭 WPF GUI 应用程序的正确方法 : GetCurrentProcess(). Kill()、Environment.Exit(0) 或 this.Shutdown()

c# - 获取用户控制 HTML