C# 以函数Func为参数的C扩展

C# 以函数Func为参数的C扩展,c#,functional-programming,extension-methods,func,chaining,C#,Functional Programming,Extension Methods,Func,Chaining,请不要与代码混淆,代码是错误的。关注下面这个大胆的问题 我一直在准备学习函数式编程,所以至少要准备好它的先决条件,我一直在学习扩展、函数和lambda表达式 下面的代码不起作用,我只是认为应该这样编码: 节目: class Program { static void Main(string[] args) { int s = 10; int t = s.CreatedMethod(2, 3); // <-- the one that call

请不要与代码混淆,代码是错误的。关注下面这个大胆的问题

我一直在准备学习函数式编程,所以至少要准备好它的先决条件,我一直在学习扩展、函数和lambda表达式

下面的代码不起作用,我只是认为应该这样编码:

节目:

class Program
{
    static void Main(string[] args)
    {
        int s = 10;
        int t = s.CreatedMethod(2, 3); // <-- the one that calls the extension
        Console.WriteLine(t.ToString());
        Console.ReadLine();
    }


    static int RegularMethod(int v1, int v2)
    {
        return v1 * v2; // <-- I also wanted to multiply int 's' like this s*v1*v2
    }
}
分机:

public static class Extension
{
    public static int CreatedMethod(this int number, Func<int, int, int> fn) 
    { 
       // I'm expecting that the code here will read the 
       // RegularMethod() above
       // But I don't know how to pass the parameter of the function being passed
       return @fn(param1, param2)// <-- don't know how to code here ??
    } 
}
如您所见,CreateMethod扩展了我的整数's'。我的计划是在上面的CreateMethod中传递这两个参数,并将这两个参数乘以's'

在上面的例子中,答案应该是60


你能用扩展帮我做吗?

这可能是你想要的,但把函数作为参数传递是没有意义的,或者我只是缺少了一些东西。无论如何,它是有效的:

class Program
{
    static void Main(string[] args)
    {
        int s = 10;
        // the function we're passing as a parameter will multiply them
        // then return the result
        int t = s.CreatedMethod((param1, param2) => param1 * param2);
        // or you can use this since the method signature matches:
        // int t = s.CreatedMethod(RegularMethod);
        Console.WriteLine(t.ToString()); // outputs "60"
        Console.ReadLine();
    }

    static int RegularMethod(int v1, int v2)
    {
        return v1 * v2; // <-- I also wanted to multiply int 's' like this s*v1*v2
    }
}

public static class Extension
{
    public static int CreatedMethod(this int number, Func<int, int, int> fn)
    {
        return number * fn.Invoke(2, 3);
    }
}
然后像这样调用调用:

fn.invoke(val1, val2)
public static int CreatedMethod(this int number1, int number2, Func<int, int, int> fn) {
    fn(number1, number2);
}

扩展可以如下所示:

fn.invoke(val1, val2)
public static int CreatedMethod(this int number1, int number2, Func<int, int, int> fn) {
    fn(number1, number2);
}
这将首先使用10和2调用RegularMethod,第二次使用20和3调用RegularMethod

另外一种方法是使用扩展,如

public static int CreatedMethod(this int number1, int number2, int number3, Func<int, int, int> fn) {
    fn(fn(number1, number2), number3);
}

Func接受2个输入参数并返回int。所以要调用它,yout应该是funparam1,param2。您的扩展只接受除fn之外的单个参数。所以您需要在CreatedMethod中添加其他参数。@DovydasSopa是的,您是对的,我忘了包含它。但即使我这么做了。我在哪里可以买到param1和param2?看看代码。不清楚你是否首先想要Func。如果你想传递数字,为什么要这样声明呢?是的,我也在想同样的问题——为什么要传递函数?你想做什么?你想做s.CreatedMethod2,3,RegularMethod吗?不清楚为什么要这样做,除非CreatedMethod也在做其他事情…我认为OP正在尝试传递Func和int参数。。。不知道为什么,但你来了。嗨,Nasredine,这段代码->fn.Invoke2,3。。这是硬编码的,对吗?我可能也会更改。是的,它们是硬编码的,除非您将CreateMethod更改为公共静态int CreatedMethod此int编号,int val1,int val2,Func fn,然后使用fn调用invoke.invokeval1,val2
var s = 10;
var t = s.CreatedMethod(2, 3, RegularMethod);