我有一堂这样的课:
public class Proxy<TClient>()
where TClient : ClientBase<TChannel>
{
}
我希望能够指定这样的内容:
where TClient : ClientBase<TChannel>
where TChannel : class
但是没有在类定义中指定它,如下所示:
public class Proxy<TClient, TChannel>()
有办法做到这一点还是我需要有上面的第二个类型定义?
最佳答案
这是不可能的。您必须包含 TChannel
作为 Proxy
的泛型类型参数。
克服这个“限制”的选项之一(用引号引起来,因为它是 C# 类型系统工作方式产生的设计特性)是使用每个 channel 都应该实现的接口(interface):
public interface IChannel { … }
public class Proxy<TClient>()
where TClient : ClientBase<IChannel>
{
}
public class MyObscureChannel : IChannel { … }
public class MyObscureClient : ChannelBase<MyObscureChannel> { … }
…
var client = new Proxy<MyObscureClient>(…); // MyObscureChannel is implied here
关于c# - 泛型类型的泛型类型约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17191162/