C# 使用泛型方法实现接口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1344694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 15:24:50  来源:igfitidea点击:

Implement an Interface with Generic Methods

c#genericsinterface

提问by Jason N. Gaylord

I'm drawing a blank on this one and can't seem to find any previous example that I wrote. I'm trying to implement a generic interface with a class. When I implement the interface I think something isn't working right because Visual Studio continually produces errors saying that I'm not implmenting all of the methods in the Generic Interface.

我在这个上面画了一个空白,似乎找不到我以前写的任何例子。我正在尝试用一个类来实现一个通用接口。当我实现该接口时,我认为有些地方无法正常工作,因为 Visual Studio 不断产生错误,指出我没有实现通用接口中的所有方法。

Here's a stub of what I'm working with:

这是我正在使用的存根:

public interface IOurTemplate<T, U>
{
    IEnumerable<T> List<T>() where T : class;
    T Get<T, U>(U id)
        where T : class
        where U : class;
}

So what should my class look like?

那么我的班级应该是什么样子的呢?

采纳答案by Reed Copsey

You should rework your interface, like so:

你应该重新设计你的界面,像这样:

public interface IOurTemplate<T, U>
        where T : class
        where U : class
{
    IEnumerable<T> List();
    T Get(U id);
}

Then, you can implement it as a generic class:

然后,您可以将其实现为泛型类:

public class OurClass<T,U> : IOurTemplate<T,U>
        where T : class
        where U : class
{
    IEnumerable<T> List()
    {
        yield return default(T); // put implementation here
    }

    T Get(U id)
    {

        return default(T); // put implementation here
    }
}

Or, you can implement it concretely:

或者,您可以具体实现它:

public class OurClass : IOurTemplate<string,MyClass>
{
    IEnumerable<string> List()
    {
        yield return "Some String"; // put implementation here
    }

    string Get(MyClass id)
    {

        return id.Name; // put implementation here
    }
}

回答by ChrisW

I think you probably want to redefine your interface like this:

我想你可能想像这样重新定义你的界面:

public interface IOurTemplate<T, U>
    where T : class
    where U : class
{
    IEnumerable<T> List();
    T Get(U id);
}

I think you want the methods to use (re-use) the generic parameters of the generic interface in which they're declared; and that you probably don't want to make them generic methods with their own (distinct from the interface's) generic parameters.

我认为您希望方法使用(重用)声明它们的通用接口的通用参数;并且您可能不想让它们成为具有自己(不同于接口的)泛型参数的泛型方法。

Given the interface as I redefined it, you can define a class like this:

给定我重新定义的接口,您可以像这样定义一个类:

class Foo : IOurTemplate<Bar, Baz>
{
    public IEnumerable<Bar> List() { ... etc... }
    public Bar Get(Baz id) { ... etc... }
}

Or define a generic class like this:

或者像这样定义一个泛型类:

class Foo<T, U> : IOurTemplate<T, U>
    where T : class
    where U : class
{
    public IEnumerable<T> List() { ... etc... }
    public T Get(U id) { ... etc... }
}

回答by Noon Silk

-- Edit

- 编辑

The other answers are better, but note you can have VS implement the interface for you, if you are confused as to how it should look.

其他答案更好,但请注意,如果您对它的外观感到困惑,您可以让 VS 为您实现该接口。

Process described below.

过程描述如下。

Well, Visual Studio tells me it should look like this:

好吧,Visual Studio 告诉我它应该是这样的:

class X : IOurTemplate<string, string>
{
    #region IOurTemplate<string,string> Members

    IEnumerable<T> IOurTemplate<string, string>.List<T>()
    {
        throw new NotImplementedException();
    }

    T IOurTemplate<string, string>.Get<T, U>(U id)
    {
        throw new NotImplementedException();
    }

    #endregion
}

Note that all I did was write interface, then click on it, and wait for the little icon to popup to have VS generate the implementation for me :)

请注意,我所做的只是编写接口,然后单击它,然后等待弹出小图标让 VS 为我生成实现:)