如何在接口中使用通用委托

发布于 2024-12-07 10:11:41 字数 799 浏览 0 评论 0原文

我正在尝试创建一个包含通用委托的接口。然后,我希望实现该接口的类决定实际的类型方法,或者最好返回另一个委托。

下面是一些描述我想要实现的目标的代码。

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
         }
    }
}

这可能吗?或者说不可能以这种方式更换代表?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

坚持沉默 2024-12-14 10:11:42

由于继承原则,您不能这样做。所有在 ITest 中工作的东西都应该在派生类/接口中工作。这意味着,如果我能够

GenericMethod<int> someMethod

在 ITest 中使用(查看 int),我应该能够在 TestA 和 TestB 中使用它。您试图忽略此限制

You cannot do this because of inheritance principles. All, that works in ITest, should work in derived classes/interfaces. 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. You are trying to ignore this restriction

溺孤伤于心 2024-12-14 10:11:42
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
        }
    }
}
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
        }
    }
}
戏剧牡丹亭 2024-12-14 10:11:41

我认为如果不使接口通用,这是不可能的。常见的实现是:

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

或者,如果您想真正拥有一个非泛型接口,请使用:

public interface ITest
{
    GenericMethod<object> someMethod;
}

您还可以查看两个接口 IEnumerableIEnumerable 了解如何组合通用和非通用接口。当您不关心具体类型时,只需显式实现非泛型接口即可使用。

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. Just implement the non-generic interface explicitly for use when you do not care about the concrete type.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文