本文介绍了Func键之间的转换不同#的类型参数的个数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有内置的方法对不同类型的函数功能的代表之间进行转换?也就是说,假设你需要一个函数功能,但你有一个Func键(和你应该通过在t参数的值)。例如:

 静态TREsult美孚< TResult>(Func键< TResult&F)的温度
{
/ / ...
TResult结果= F();
// ...
返回结果;
}

静态INT MYFUNC(int i)以
{
回报我;
}

无效CallFoo()
{
Func键< INT> FUNC = ConvertFunc(MYFUNC,1); //这个家庭的方法存在吗?
INT J =美孚(FUNC);
}



我写我自己的,是这样的:

 静态Func键< TResult> ConvertFunc< T,TResult>(Func键< T,TResult> F1,T T)
{
回报率()=> F1(T);
}

静态Func键< TResult> ConvertFunc&所述; T1,T2,TResult>(Func键&下; T1,T2,TResult> F2,T1 T1,T2 T2)
{
返回()=> F2(T1,T2);
}

//等



但我想知道如果一个家庭的方法,这样的存在(或者即使有更好的方式来做到这一点)。



从本质上讲,我做这那里是一个案例一些锅炉板代码的方法中,随后通过一个函数调用(其中在功能的数量和种类将有所不同,但返回类型是相同的)中,随后更锅炉板代码



所有意见的欢迎!谢谢


解决方案

 静态Func键< TResult> ConvertFunc< T,TResult>(Func键< T,TResult> F1,T T)
{
回报率()=> F1(T);
}

这样的代码对我看起来有点危险 - 而不是本身就是什么错的,但的需要小心的。您使用的闭合嵌入在函数的输入变量。但是,这可能会导致困难的bug,因为如果之间的转换函数功能和变量的变化运行它,结果会有所不同。



我只是好奇,会是什么益处。你是不是想从功能的消费者遁形输入参数?只要变量是本地一家传递给它,就可以了。



在溶液而言,就绝非一因为.NET创建16个不同的通用 Func键<> 正是出于同样的原因



您也许可以使用反射,以实现一个解决方案,但你会付罚款调用的功能。 MethodInfo.GetGenericArguments()会给你的类型,然后你可以使用 MethodInfo.MakeGenericMethod()以创建新的






更新



只是为了说明我的观点:

 静态INT双(INT数)
{
返回数* 2;
}

静态无效的主要(字串[] args)
{

INT I = 2;
Func键< INT> F =()=>双(I)
I = 3;
Console.WriteLine(F()); //输出6,而不是4

}


Are there built in methods for converting between the various types of Func delegates? That is, suppose you need a Func, but you have a Func (and you have the value that should be passed in for the T parameter). For example:

static TREsult Foo<TResult>(Func<TResult> f)
{
   // ...
   TResult result = f();
   // ...
   return result;
}

static int MyFunc(int i)
{
    return i;
}

void CallFoo()
{
    Func<int> func = ConvertFunc(MyFunc, 1); // Does this family of methods exist?
    int j = Foo(func);
}

I've written my own, like this:

    static Func<TResult> ConvertFunc<T, TResult>(Func<T, TResult> f1, T t)
    {
        return () => f1(t);
    }

    static Func<TResult> ConvertFunc<T1, T2, TResult>(Func<T1, T2, TResult> f2, T1 t1, T2 t2)
    {
        return () => f2(t1, t2);
    }

    // etc.

But I'm wondering if a family of methods like this exists (or even if there's a better way to do this).

Essentially, I'm doing this for a case where there is some boiler plate code in a method followed by a function call (where the number and types in the function will vary, but the return type is the same), followed by more boiler plate code.

All opinions welcome! Thanks.

解决方案
static Func<TResult> ConvertFunc<T, TResult>(Func<T, TResult> f1, T t)
{
    return () => f1(t);
}

This kind of code to me looks a bit dangerous - not that by itself is anything wrong but need to be careful. You are using closure to embed an input variable in the function. But this could lead to difficult bugs since if the variable changes between converting Func and running it, the result would be different.

I am just curious what would be the benefit. Are you trying to hide away input parameter from the consumer of the function? As long as the variable is a local one passed to it, would be fine.

In terms of a solution, there would not be one since .NET has created 16 different generic Func<> exactly for the same reason.

You can perhaps use reflection to implement a solution but you would be paying a penalty for calling the functions. MethodInfo.GetGenericArguments() would give you the types and you then can use MethodInfo.MakeGenericMethod() to create new ones.


Update

Just to illustrate my point:

    static int Double(int number)
    {
        return number * 2;
    }

    static void Main(string[] args)
    {

        int i = 2;
        Func<int> f = () => Double(i);
        i = 3;
        Console.WriteLine(f()); // prints 6 and not 4

    }

这篇关于Func键之间的转换不同#的类型参数的个数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 16:35