简体   繁体   English

如何使用Func <>返回方法的引用?

[英]How do you return a reference of a method with Func<>?

I have a simple bit of code but am struggling to work out the correct syntax to return the method as a reference so it can be used. 我有一些简单的代码,但是正在努力找出正确的语法以将该方法作为参考返回,以便可以使用它。

This is what i am trying to do: 这就是我想要做的:

    public static Func<float> GetFunction(Functions functions)
    {
        switch (functions)
        {
            case Functions.Linear: return // how do i return the func here?
                break;
            default: return null;
        }
    }

    public static float Linear(float k) // am trying to return this
    {
        return k;
    }

The idea then is i will be able to do something like: 然后的想法是我将能够做类似的事情:

Func<float> func = GetFunction(Functions.Linear);
print func(10);

I am not sure how i create the return though. 我不确定如何创建回报。

For a delegate to float Linear(float k) , a function that takes a float argument and has a float return value, the generic Func<float, float> delegate would have to be used instead of Func<float> , which encapsulates a function that takes no argument and returns a float . 若要使float Linear(float k)这个具有float参数并具有float返回值的函数,则必须使用泛型Func<float, float>委托代替封装了一个函数的Func<float>没有参数并返回float As in: 如:

public static Func<float, float> GetFunction(Functions functions)
{
    switch (functions)
    {
        case Functions.Linear: return Linear;
            break;
        default: return null;
    }
}

public static float Linear(float k) // am trying to return this
{
    return k;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 您如何返回Func <Func<string, int> ,int&gt; - How do you return a Func<Func<string, int>, int> 你如何通过获取; 属性的方法到接受Func的方法? - How do you pass in the get; method of a property to a method that accepts a Func? 如何声明具有匿名返回类型的 Func? - How do you declare a Func with an anonymous return type? 您如何退回原始列表的副本<t>来自 Func<t, tresult> ?</t,></t> - How do you return a copy of original List<T> from Func<T, TResult>? 如何为Func创建Moq模拟 - How do you create a Moq mock for a Func 如何使用表达式设置方法的返回类型<func<tmodel, tproperty> &gt; </func<tmodel,> - How do I set return type from method with an Expression<Func<TModel, TProperty>> 如何编写C#方法以返回Func <T> 基于枚举参数? - How do I write a C# method to return a Func<T> based in an enum parameter? MVC 3 - 如何从动作方法返回显示模板? - MVC 3 - How do you return a Display Template from an action method? 如何使用ReSharper SDK确定方法的返回类型? - How do you determine the return type of a method using the ReSharper SDK? 你如何从方法中注入返回值 - How do you inject a return value from a method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM