我有一个通用的List-Class,我想从这个类中使用我的Method CorrectData()。 目前我已将Method CorrectData()实现到类LoadListe中。如果我把它放入通用列表中,那么我得到编译器错误CS1061。
怎么了?
由于 斯特芬
using System.Collections.ObjectModel;
namespace WindowsFormsGenerics
{
/// <summary>Basisklasse für alle Elemente </summary>
public class Base
{
/// <summary> Elementname </summary>
public string Name { get; set; }
/// <summary> Beschreibung </summary>
public string Description { get; set; }
}
public class Knoten : Base { }
public class OneNodeElement : Base
{
/// <summary> KnotenOne des Elementes </summary>
public Knoten KnotenOne { get; set; }
/// <summary> Schaltzustand am KnotenOne </summary>
public bool SwitchOne { get; set; }
}
public class Load : OneNodeElement
{
public void CorrectData(){}
}
public sealed class LoadListe : Liste<Load>
{
public void CorrectData()
{
foreach (Load item in this)
{
item.CorrectData();
}
}
}
public abstract class Liste<T> : Collection<T> where T : Base
{
public T GetItem(string searchsString, string parameter = "NAME")
{
return null;
}
public void CorrectData()
{
foreach (T item in this)
{
item.CorrectData();
}
}
}
}
答案 0 :(得分:1)
您定义where T : Base
,然后想要在该类型的项目上调用CorrectData()
。这种方法没有在Base
类中实现。
该方法在Load
类中实现,因此您可以使用where T: Load
代替CorrectData()
类中的Base
方法。
答案 1 :(得分:0)
您缺少CorrectData
实现或至少Base
类内的定义。您可以像这样定义Base
类:
public class Base
{
/// <summary> Elementname </summary>
public string Name { get; set; }
/// <summary> Beschreibung </summary>
public string Description { get; set; }
public virtual void CorrectData() { }
}
然后在后代中覆盖CorrectData
方法,如下所示:
public class Load : OneNodeElement
{
public override void CorrectData() { }
}