简体   繁体   English

如何在接口中使用泛型委托

[英]How to use a generic delegate in an interface

I'm trying to create an interface holding a generic delegate. 我正在尝试创建一个包含通用委托的接口。 I then want the classes implementing the interface to decide the actual type method, or preferably even return another delegate. 然后,我希望实现该接口的类决定实际的类型方法,或者最好甚至返回另一个委托。

Below are some code describing what I'm trying to acheive. 以下是一些代码,描述了我要达到的目标。

public delegate void GenericMethod<T>(T arg);
public delegate void StringMethod(string str);
public delegate void ByteMethod(byte bt);

public interface ITest
{
    GenericMethod<T> someMethod;    
}

public class TestA : ITest
{
    public GenericMethod<string> someMethod
    {
         get 
         {
               return stringMethod; //which is of type StringMethod(string str), defined above
         }
    }
}

public class TestB : ITest
{
    public GenericMethod<byte> someMethod
    {
         get 
         {
               return byteMethod; //which is of type ByteMethod(byte bt);, defined above
         }
    }
}

Is this possible? 这可能吗? Or is it impossible to switch delegates in such a manner? 还是不可能以这种方式切换代表?

I do not think that this is possible without making the interface generic. 我认为如果不使接口通用,就不可能做到这一点。 The common implementation would be: 常见的实现方式是:

public interface ITest<T>
{
    GenericMethod<T> someMethod;
}

Or, if you want to actually have a non-generic interface, use: 或者,如果您想真正拥有一个非通用接口,请使用:

public interface ITest
{
    GenericMethod<object> someMethod;
}

You can also take a look at two interfaces IEnumerable and IEnumerable<T> to see how you can combine both generic and non-generic interfaces. 您还可以查看IEnumerableIEnumerable<T>两个接口,以了解如何组合通用接口和非通用接口。 Just implement the non-generic interface explicitly for use when you do not care about the concrete type. 只要不关心具体类型,就可以显式实现非泛型接口以供使用。

public delegate void GenericMethod<T>(T arg);
public delegate void StringMethod(string str);
public delegate void ByteMethod(byte bt);

public interface ITest<T>
{
    GenericMethod<T> someMethod { get; };
}

public class TestA : ITest<string>
{
    public GenericMethod<string> someMethod
    {
        get
        {
            return stringMethod; //which is of type StringMethod(string str), defined above
        }
    }
}

public class TestB : ITest<byte>
{
    public GenericMethod<byte> someMethod
    {
        get
        {
            return byteMethod; //which is of type ByteMethod(byte bt);, defined above
        }
    }
}

You cannot do this because of inheritance principles. 由于继承原则,您无法执行此操作。 All, that works in ITest, should work in derived classes/interfaces. 所有可以在ITest中工作的方法都应该在派生类/接口中工作。 That means, if I'm able to use 也就是说,如果我能够使用

GenericMethod<int> someMethod

(look at int) in ITest, I should be able to use it in TestA and TestB. (看int)在ITest中,我应该能够在TestA和TestB中使用它。 You are trying to ignore this restriction 您正在尝试忽略此限制

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

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