简体   繁体   English

如何使用Func <T,bool> 传入对象?

[英]How can I used Func<T,bool> that passed in object?

I want to create IValueConverter that getting function and object (of some kind) and return bool value. 我想创建IValueConverter来获取函数和对象(某种形式)并返回布尔值。

How can I cast function and parameters in runtime? 如何在运行时强制转换函数和参数?

public object EvaluateByValue(object MyFunc, object parameter)
{
    return MyFunc(parameter);
}

For example: 例如:

Func<int,bool> MyIntFunction =  i=>return i%2;int number = 8;
bool IsEvan = (bool)EvaluateByValue(MyIntFunction,8);

and another use can be: 另一个用途是:

Func<string,bool> MyStringFunction = txt=>txt=="Hello";

bool IsWelcome = (bool)EvaluateByValue(MyStringFunction,"Goodbye");

Is there any way to cast the "Func" method by the parameter type? 有什么方法可以通过参数类型强制转换“ Func”方法?

Thanks 谢谢

I would change your method to the following: 我将您的方法更改为以下内容:

public object EvaluateByValue(Delegate MyFunc, params object[] parameters)
{
    return MyFunc.DynamicInvoke(parameters);
}

If the cast is your only problem, perhaps what you're looking for is: 如果演员表是您唯一的问题,也许您正在寻找的是:

var myFuncDelegate = (Func<object, bool>)parameter;

If you want the Func's parameter type to be the same as the given parameter, you would have to use reflection to cast the delegate and execute it. 如果希望Func的参数类型与给定参数相同,则必须使用反射来强制转换并执行委托。

Well the minimum amount of change would be: 好吧, 最小更改量为:

public object EvaluateByValue(Func<object, object> MyFunc, object parameter)
{
    return MyFunc(parameter);
}

or to allow multiple paramaters: 或允许多个参数:

public object EvaluateByValue(Func<object[], object> MyFunc, params object[] parameter)
{
    return MyFunc(parameter);
}

Although I'm not sure what you're trying to do. 尽管我不确定您要做什么。

You could do something like this for the function declaration 您可以对函数声明执行以下操作

 static K EvaluateByValue<T, K>(Func<T, K> MyFunc, T parameter)
        {
            return MyFunc(parameter);
        }

T is the parameter type and K is the return type T是参数类型, K是返回类型

So, let's say you've got this function you want to pass in 因此,假设您有要传递的功能

 static string parameterFunction(string param)
        {
            return param + " World!";
        }

you would call it like so 你会这样称呼它

var answer = EvaluateByValue<string, string>(parameterFunction, "Hello");

And answer would be Hello World! answer将是“ Hello World!

暂无
暂无

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

相关问题 我怎么能做一个Func <object[],Expression<Func<T,bool> &gt;&gt;动态? - How can I do a Func<object[],Expression<Func<T,bool>>> dynamic? 如何使用Func <T, bool> 作为Func的参数 <object, bool> ? - How to use Func<T, bool> as a parameter for Func<object, bool>? 我可以动态创建一个表达式 <Func<T, bool> &gt;谓词,但我如何创建表达式 <Func<T1,T2,bool> &gt; - I can dynamically create an Expression<Func<T, bool>> predicate ,but how do i create Expression<Func<T1,T2,bool>> 如何提取传递给Expression的属性名称和值 <Func<T,bool> &gt;? - How do I extract the property name and value being passed into an Expression<Func<T,bool>>? 如何转换 Linq 表达式<func<object,object,bool> &gt; 表达<func<t1,t2,bool> &gt; </func<t1,t2,bool></func<object,object,bool> - How to convert Linq Expression<Func<object,object,bool>> to Expression<Func<T1,T2,bool>> 我可以使用表情 <Func<T, bool> &gt;并可靠地查看Func中引用了哪些属性 <T, bool> ? - Can I use Expression<Func<T, bool>> and reliably see which properties are referenced in the Func<T, bool>? 如何从IQueryable进行投射 <T> 到IQueryable <U>和</u> <Func<T, bool> <U>&gt;至</u> <Func<U, bool> <U>&gt;好玩吗?</u> - How do I cast from IQueryable<T> to IQueryable<U> and <Func<T, bool>> to <Func<U, bool>> for fun? 无法转换func类型 <T,bool> 布尔 - can not convert type func<T,bool> to bool 如何获取F的Func中使用的属性名称字符串 - How can I get property name strings used in a Func of T 如何转换Func <T,bool> 表达 <Func<T,bool> &gt; - How to convert Func<T,bool> to Expression<Func<T,bool>>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM