通用方法导致C#编译器错误CS1061 [英] C# compiler error CS1061 by generic Method

查看:591
本文介绍了通用方法导致C#编译器错误CS1061的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通用的List-Class,我想使用此类中的Method CorrectData(). 目前,我已经将方法CorrectData()实现到LoadListe类中.如果将其放入通用列表中,则会出现编译器错误CS1061.

I have an generic List-Class and I want to use my Method CorrectData() from this class. At the moment I have implementated the Method CorrectData() into the class LoadListe. If I put this into the generic List then I get an compiler error 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();
            }
        }
    }
} 

推荐答案

您缺少CorrectData实现或至少在Base类中没有定义.您可以这样定义您的Base类:

You are missing CorrectData implementation or at least definition inside Base class. You could define your Base class like this:

public class Base
{
    /// <summary> Elementname </summary>
    public string Name { get; set; }

    /// <summary> Beschreibung </summary>
    public string Description { get; set; }

    public virtual void CorrectData() { }
}

然后在后代中覆盖CorrectData方法,如下所示:

And then override CorrectData method in descendants, like this:

public class Load : OneNodeElement
{
    public override void CorrectData() { }
}

这篇关于通用方法导致C#编译器错误CS1061的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆