c# 如何对方法签名中的泛型 Func 参数施加约束


public void Test<TFeature>(Func<TController, ViewResult> controllerAction)                                          
            where TController : IController
            where TFeature : ISecurityFeature
        {
            ...
        }

我收到错误,测试未定义类型参数 TController。 如何对财务总监施加约束?

除非你在SomeClass<TController>内部定义它(在这种情况下你需要在class SomeClass<TController>旁边放置约束(,否则你需要TController函数的泛型参数,即:

public void Test<TFeature, TController>(Func<TController, ViewResult> controllerAction)                                          
            where TController : IController
            where TFeature : ISecurityFeature
        {
            ...
        }

您还需要将 TController 添加为通用参数

public void Test<TFeature, TController>(Func<TController, ViewResult> controllerAction)                                          
        where TController : IController
        where TFeature : ISecurityFeature
    {
        ...
    }

最新更新