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

时间:2012-02-15 22:40:48

标签: c# generics

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

我收到错误, Test没有定义类型参数TController 。如何在TController上设置约束?

2 个答案:

答案 0 :(得分:5)

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

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

答案 1 :(得分:5)

除非你在SomeClass<TController>中定义它(在这种情况下你需要在class SomeClass<TController>旁边放置约束),你需要使TController成为你函数的泛型参数,即:

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