C#泛型:如果T是返回类型,它也可以是void吗?如何将这些接口组合在一起? [英] C# Generics: If T is a return type, can it also be void? How can I combine these interfaces together?

查看:29
本文介绍了C#泛型:如果T是返回类型,它也可以是void吗?如何将这些接口组合在一起?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下接口使用回调返回 T 类型的泛型参数...

I have the following interface that returns the generic parameter of type T using a callback...

public interface IDoWork<T>
{
    T DoWork();
}

但是我也有以下接口,但它不会调用回调,因为它返回 void.

however I also have the following interface as well, but it won't invoke a callback since it returns void.

public interface IDoWork
{
    void DoWork();
}

我可以将这两个接口结合起来并使用运行时逻辑来确定差异吗?我该怎么做?

Can I combine these two interfaces and use runtime logic to determine the difference? How can I do that?

推荐答案

很遗憾,它们不能合并.

Unfortunately, they can't be combined.

您可以在框架中看到这一点 - 这就是为什么有一个单独的 TaskTask 类,例如.

You can see this in the framework - that's why there is a separate Task and Task<T> class, for example.

话虽如此,您通常可以通过使用 IDoWork 并为值等传递 null 来共享此类场景中的实现.

That being said, you can often share implementations in this type of scenario by using IDoWork<object> and passing null for values, etc.

这篇关于C#泛型:如果T是返回类型,它也可以是void吗?如何将这些接口组合在一起?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆