How create a generic Func delegate

2019-05-11 01:06发布

问题:

I have this method

    public static T F<T>(T arg)
    {
        return arg;
    }

and I want to create a Func delegate to F. I try this

public Func<T, T> FuncF = F;

but it's not syntactically correct. How I can do that.

回答1:

Only classes and methods can be generic in and of themselves. A field that uses generic parameters must be within a generic class context:

public class Test<T>
{
    public static T F(T arg)
    {
        return arg;
    }

    public Func<T, T> FuncF = F;
}

Or if the type parameter for F should not be connected to FuncF, then just use a different name for one of the parameters:

public class Test<T>
{
    public static U F<U>(U arg)
    {
        return arg;
    }

    public Func<T, T> FuncF = F;
}


回答2:

F<int> is a method which would be assignable to a Func<int, int> variable.

F<double> is a different method which would be assignable to a Func<double, double> variable.

But you cannot have a generic variables. C# just doesn't work like that.

The closest thing to generic variables is a field of a generic class:

static class DelegateContainer<T>
{
  public static Func<T, T> FuncF = F;
}


回答3:

You can only (re)expose generics from a generic class, or in a generic method. Otherwise, you will need to provide definite types for T (e.g. for a local variable or as a field or property in a non-generic class). Examples:

// I.e. Re-expose by wrapping your Func in a static class:
public static class MyFuncs
{
    public static T F<T>(T arg)
    {
        return arg;
    }
}

public class Generics<T>
{
    // Use it as a field
    private Func<T, T> f = MyFuncs.F;

    // Or getter func
    public Func<T, T> FuncF()
    {
        return MyFuncs.F;
    }
}

// Re-expose generic via Generic Method
public class GenericMethod
{
    public Func<T, T> FuncF<T>()
    {
        return MyFuncs.F;
    }
}


// i.e. Consume the generic and do not re-expose it
public class SpecificClass
{
    public Foo FuncF(Foo f)
    {
        return MyFuncs.F<Foo>(f); // <Foo> is optional - compiler can infer
    }
}