C# 如何继承具有不同泛型返回类型的方法

C# 如何继承具有不同泛型返回类型的方法,c#,generics,C#,Generics,我试图继承一个返回ServerType类型的泛型BindingList的方法。例如,假设我有以下内容: public interface IServer { string IpAddress { get; set; } string Name { get; set; } string HostName { get; set; } string OsVersion { get; set; } } public class BaseServer : IServ

我试图继承一个返回ServerType类型的泛型BindingList的方法。例如,假设我有以下内容:

public interface IServer 
{

    string IpAddress { get; set; }
    string Name { get; set; }
    string HostName { get; set; }
    string OsVersion { get; set; }

}

public class BaseServer : IServer
{
    private string _IpAddress;
    private string _Name;
    private string _HostName;
    private string _OsVersion;

    public string IpAddress
    {
        get { return _IpAddress; }
        set { _IpAddress = value; }
    }

    public string Name
    {
        get { return _Name; }
        set { _Name = value; }
    }

    public string HostName
    {
        get { return _HostName; }
        set { _HostName = value; }
    }

    public string OsVersion
    {
        get { return _OsVersion; }
        set { _OsVersion = value; }
    }
}

public class ServerTypeA : BaseServer { }
public class ServerTypeB : BaseServer { }
public class ServerTypeC : BaseServer { }

public class ServerTypeList : List<ServerTypeA>
{ 

    public BindingList<ServerTypeA> ToBindingList()
    {
        BindingList<ServerTypeA> myBindingList = new BindingList<ServerTypeA>();

        foreach (ServerTypeA item in this.ToList<ServerTypeA>())
        {
            _bl.Add(item);
        }

        return _bl;

    }   
}
公共接口IServer
{
字符串IpAddress{get;set;}
字符串名称{get;set;}
字符串主机名{get;set;}
字符串OsVersion{get;set;}
}
公共类BaseServer:IServer
{
私有字符串\u IpAddress;
私有字符串\u名称;
私有字符串\u主机名;
私有字符串_OsVersion;
公共字符串IP地址
{
获取{return\u IpAddress;}
设置{u IpAddress=value;}
}
公共字符串名
{
获取{return\u Name;}
设置{u Name=value;}
}
公共字符串主机名
{
获取{return\u HostName;}
设置{u HostName=value;}
}
公共字符串版本
{
获取{return\u OsVersion;}
设置{u OsVersion=value;}
}
}
公共类ServerTypeA:BaseServer{}
公共类ServerTypeB:BaseServer{}
公共类ServerTypeC:BaseServer{}
公共类ServerTypeList:List
{ 
公共绑定列表到绑定列表()
{
BindingList myBindingList=新建BindingList();
foreach(此.ToList()中的ServerTypeA项)
{
_bl.Add(项目);
}
返回_bl;
}   
}

是否有任何方法可以执行“ToBindingList”方法,而不必在每个派生的服务器类中重复它,并让它使用正确的泛型类型。

首先,不要派生自
列表。而是使用它()

然后使您的
存储库
-类成为通用类:

public class Repository : Server
{ 

}

public class Repositories<T> where T: Server
{ 

    private List<T> theList = new List<T>();

    public Repositories<T>(List<T> theList) this.theList = theList; }

    public BindingList<T> ToBindingList()
    {
        BindingList<T> myBindingList = new BindingList<T>();

        foreach (Titem in this.theList)
        {
            _bl.Add(item);
        }

        return _bl;

    }   
}
公共类存储库:服务器
{ 
}
公共类存储库,其中T:Server
{ 
私有列表列表=新列表();
公共存储库(列表)this.theList=theList;}
公共绑定列表到绑定列表()
{
BindingList myBindingList=新建BindingList();
foreach(此列表中的Titem)
{
_bl.Add(项目);
}
返回_bl;
}   
}

现在,您可以拥有
存储库
——从
服务器
派生的任意类的实例

首先不要从
列表
派生。而是使用它()

然后使您的
存储库
-类成为通用类:

public class Repository : Server
{ 

}

public class Repositories<T> where T: Server
{ 

    private List<T> theList = new List<T>();

    public Repositories<T>(List<T> theList) this.theList = theList; }

    public BindingList<T> ToBindingList()
    {
        BindingList<T> myBindingList = new BindingList<T>();

        foreach (Titem in this.theList)
        {
            _bl.Add(item);
        }

        return _bl;

    }   
}
公共类存储库:服务器
{ 
}
公共类存储库,其中T:Server
{ 
私有列表列表=新列表();
公共存储库(列表)this.theList=theList;}
公共绑定列表到绑定列表()
{
BindingList myBindingList=新建BindingList();
foreach(此列表中的Titem)
{
_bl.Add(项目);
}
返回_bl;
}   
}

现在您可以拥有
存储库
——从
服务器
派生的任意类的实例。首先,为所有集合创建一个基本列表:

public class MyListBase<T> : List<T>
    where T: Server
{ 
    public BindingList<T> ToBindingList()
    {
        BindingList<T> myBindingList = new BindingList<T>();
        foreach (T item in this.ToList<T>())
            myBindingList.Add(item);
        return myBindingList;
    }   
}
公共类MyListBase:列表
其中T:Server
{ 
公共绑定列表到绑定列表()
{
BindingList myBindingList=新建BindingList();
foreach(此.ToList()中的T项)
myBindingList.Add(项);
返回myBindingList;
}   
}
然后使用此项继承自:

public class Repositories : MyListBase<Repository>
{
}
公共类存储库:MyListBase
{
}

首先,为所有收藏创建一个基本列表:

public class MyListBase<T> : List<T>
    where T: Server
{ 
    public BindingList<T> ToBindingList()
    {
        BindingList<T> myBindingList = new BindingList<T>();
        foreach (T item in this.ToList<T>())
            myBindingList.Add(item);
        return myBindingList;
    }   
}
公共类MyListBase:列表
其中T:Server
{ 
公共绑定列表到绑定列表()
{
BindingList myBindingList=新建BindingList();
foreach(此.ToList()中的T项)
myBindingList.Add(项);
返回myBindingList;
}   
}
然后使用此项继承自:

public class Repositories : MyListBase<Repository>
{
}
公共类存储库:MyListBase
{
}

ToBindingList方法只是将列表转换为BindingList。您可以为List编写一个简单的扩展方法来实现这一点。它与服务器类或代码中的任何其他内容无关。看起来您的实现不需要重复
ToBindingList()
implementation。我遗漏了什么吗?我还需要一个绑定列表。不同的存储库类型。所以我需要能够做一些类似BindingList ToBindingList()的事情ToBindingList方法只是将列表转换为BindingList。您可以为List编写一个简单的扩展方法来实现这一点。它与服务器类或代码中的任何其他内容无关。看起来您的实现不需要重复
ToBindingList()
implementation。我遗漏了什么吗?我还需要一个绑定列表。不同的存储库类型。所以我需要能够做一些类似BindingList到BindingList()的事情谢谢!这起作用了。不过,我对foreach循环做了一点修改,并用“T”替换了您的“Repository”数据类型。谢谢!这起作用了。不过,我对foreach循环做了一点修改,并将您拥有的“Repository”数据类型替换为“T”。