c# - 其中 T : <interface> into where T: class gives cs0452

标签 c# generics

我有

public static void SecureTcpRpc<InterfaceType>(string uri, 
                                               Action<InterfaceType> action) 
                                              where InterfaceType : class;

然后我在这里使用它

 private static AuthorizedActionResult 
                RunChannelAction<T>(IEnumerable<string> uris, 
                                    Func<T, AuthorizedActionResult> actionFunc)
                                    where T : IPingable
            {
                    WcfClient.SecureTcpRpc<T>....

编译器不喜欢我将 T 限制为可 IPing。我不明白它为什么反对。 IPingable 是引用类型,因此它与 SecureTpcRpc 方法上的约束相匹配。但是编译器说“T 必须是引用类型”

最佳答案

我认为您还需要对 AuthorizedActionResult 函数进行“类”约束才能使其正常工作。

where T : class, IPingable

关于c# - 其中 T : <interface> into where T: class gives cs0452,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10376383/

相关文章:

c# - 无法将类型 'UnityEngine.Vector2?' 隐式转换为 'UnityEngine.Vector2'

c# - c#关闭excel的问题

java - 将自定义类对象传递给 Java 中的泛型类型

c# - 反射、泛型、事件处理程序和委托(delegate)问题

c++ - 类如何从基于自身的模板继承?

c# - 无法使用 RestSharp 发送 cookie

C# ASP.Net - Global.asax 充满错误

c# - 在域驱动设计的项目中,您将验证放在哪里?

java - 迭代通用枚举实例

java - 泛型扩展