其中T:<interface>到T:class给出cs0452

pm1*_*100 3 c# generics

我有

public static void SecureTcpRpc<InterfaceType>(string uri, 
                                               Action<InterfaceType> action) 
                                              where InterfaceType : class;
Run Code Online (Sandbox Code Playgroud)

然后我在这里使用它

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

编译器不喜欢我将T限制为IPingable.我不明白为什么它反对.IPingable是一种引用类型,因此它匹配SecureTpcRpc方法的约束.但是编译器说"T必须是引用类型"

Mag*_*nus 5

我认为你需要AuthorizedActionResult函数的"类"约束才能使它工作.

where T : class, IPingable
Run Code Online (Sandbox Code Playgroud)