c# – 使用参数在expression.call中调用静态方法

c# – 使用参数在expression.call中调用静态方法,第1张

概述我已经扩展了Contains方法的字符串类.我试图在Expression.Call中调用它,但如何正确传递参数? 代码:String包含方法: public static class StringExts{ public static bool NewContains(this string source, string ValToCheck, StringComparison StrC 我已经扩展了Contains方法的字符串类.我试图在Expression.Call中调用它,但如何正确传递参数?

代码:String包含方法:

public static class StringExts{    public static bool NewContains(this string source,string Valtocheck,StringComparison StrComp)    {        return source.IndexOf(Valtocheck,StrComp) >= 0;    }}

在表达式中调用为:

public class Person { public string name {get; set;} }public class Persons {     public List<Person> lstPersons {get; set;}     public Persons() {      lstPersons = new List<Person>();        }}public class Filter {    public string ID { get; set; }    public Operator Operator { get; set; }    public string value { get; set; }}public voID Main(){   //Get the Json.   //"Filters": [{"ID": "name","operator": "contains","value": "Microsoft"}]    Filter Rules = JsonConvert.DeserializeObject<Filter>(Json);   // Get the List of person firstname.    List<Person> lstPerson = GetFirstname();   ParameterExpression param = Expression.Parameter(typeof(Person),"p");   Expression exp = null;   exp = GetExpression(param,rules[0]);   //get all the name contains "john" or "John"   var filteredCollection = lstPerson.Where(exp).ToList();}private Expression GetExpression(ParameterExpression param,Filter filter){   MemberExpression member = Expression.Property(param,filter.ID);   ConstantExpression constant = Expression.Constant(filter.value);   Expression bEXP = null;   switch (filter.Operator)    {         case Operator.contains:           MethodInfo miContain = typeof(StringExts).getmethod("NewContains",BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);           return  Expression.Call(miContain,member,constant,Expression.Constant(StringComparison.OrdinalignoreCase));;            break;    } }

错误:

An unhandled exception of type ‘System.ArgumentException’ occurred in System.Core.dll.Additional information: Static method requires null instance,non-static method requires non-null instance.

@H_403_15@

如何调用miContain中的参数来跟随Call()方法?

我已经更新了守则.

解决方法 您没有指定所有参数.如果为所有人创建表达式,它可以工作:
ParameterExpression source = Expression.Parameter(typeof(string));string Valtocheck = "A";StringComparison StrComp = StringComparison.CurrentCultureIgnoreCase;MethodInfo miContain = typeof(StringExts).getmethod("NewContains",BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);var bEXP = Expression.Call(miContain,source,Expression.Constant(Valtocheck),Expression.Constant(StrComp));var lambda = Expression.Lambda<Func<string,bool>>(bEXP,source);bool b = lambda.Compile().Invoke("a");
总结

以上是内存溢出为你收集整理的c# – 使用参数在expression.call中调用静态方法全部内容,希望文章能够帮你解决c# – 使用参数在expression.call中调用静态方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1263418.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存