简体   繁体   English

C#中通用接口的实现

[英]Implementation of generic interfaces in c#

I am new to c# and trying to learn generic interfaces. 我是C#的新手,正在尝试学习通用接口。 I am getting an error when trying to implement GetComments. 尝试实现GetComments时出现错误。 Error: 'cannot convert expression type 'WallGetObject' to return type T' . 错误: 'cannot convert expression type 'WallGetObject' to return type T' Is it possible to do something like that? 可以做这样的事情吗? Thanks. 谢谢。

interface IInterface
{
    T GetComments<T>(int id);
}


public class A: IInterface
{
    public T GetComments<T>()
    {
        WallGetObject item
        //... getting item
        return item
    }
}

public class B : IInterface
{
     public T GetComments<T>()
      {
        TopicFeedObject item
        //... getting item
        return item
    }
}


public class MainClass()
{

var item1 = new A();
var item2 = new B();

var list = new List<IInterface>();
list.Add(item1);
list.Add(item2);


foreach (var i in list)
{
  i.GetComments(5);
}

On first glance, I believe what you are trying to do is implement an interface method that returns a different type for each concrete class. 乍一看,我相信您想要做的是实现一个接口方法,该方法为每个具体类返回不同的类型。 That is, A.GetComments should return WallGetObject and B.GetComments() should return a TopicFeedObject . 也就是说, A.GetComments应该返回WallGetObjectB.GetComments()应该返回TopicFeedObject This would be accomplished by using a generic interface (not a generic method as you have). 这可以通过使用通用接口 (而不是您所拥有的通用方法 )来实现。

interface IInterface<T>
{
    T GetComments(int id);
}

public class A: IInterface<WallGetObject>
{
    public WallGetObject GetComments()
    {
        WallGetObject item
        //... getting item
        return item
    }
}

However, this will then lead to a problem later on when you try to create a generic list of different types of IInterface s . 但是,这将在以后尝试创建不同类型IInterface的通用列表时导致问题 That is, creating a List<IInterface> requires a common base class/interface, and is simply not possible with the above. 也就是说,创建List<IInterface>需要一个公共的基类/接口,而上述操作根本不可能实现。


The real answer is not to use generics here, but instead a base (abstract) class or interface on WallGetObject and TopicFeedObject that exposes the common functionality you need. 真正的答案不是在此处使用泛型 ,而是在WallGetObjectTopicFeedObject上使用基类(抽象)或接口来公开所需的通用功能。 For example. 例如。

interface IComments {}

class WallGetObject : IComments {}
class TopicFeedObject : IComments {}

interface IInterface
{
    IComments GetComments(int id);
}

public class A: IInterface
{
    public IComments GetComments()
    {
        WallGetObject item
        //... getting item
        return item
    }
}

public class B : IInterface
{
     public IComments GetComments()
     {
        TopicFeedObject item
        //... getting item
        return item
    }
}

You should declare and implement your interface like this. 您应该像这样声明和实现您的接口。 Note the generic type definition is moved to interface declaration instead of method declaration. 请注意,泛型类型定义移至接口声明而不是方法声明。

interface IInterface<T>
{
    T GetComments(int id);
}


public class A: IInterface<WallGetObject>
{
    public WallGetObject GetComments(int id)
    {
        WallGetObject item
        //... getting item
        return item
    }
}

public class B : IInterface<TopicFeedObject>
{
     public TopicFeedObject GetComments(int id)
     {
        TopicFeedObject item
        //... getting item
        return item
     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM