c# – 从字符串创建属性选择器表达式
发布时间:2020-12-15 18:19:58 所属栏目:百科 来源:网络整理
导读:我正在尝试从字符串生成“属性选择器”. 让我用一个真实的例子来解释一下自己: 我们有一个Person类,其中包含Name(string)属性. 我可以像这个propertySelector一样手动创建一个“属性选择器”: ExpressionFuncPerson,string propertySelector = x = x.Name;
我正在尝试从字符串生成“属性选择器”.
让我用一个真实的例子来解释一下自己: 我们有一个Person类,其中包含Name(string)属性. 我可以像这个propertySelector一样手动创建一个“属性选择器”: Expression<Func<Person,string>> propertySelector = x => x.Name; 但我想用我的方法获得相同的属性选择器. var propertySelector = CreatePropertySelectorExpression<Person,string>("Name"); 到目前为止我所拥有的是: public static Expression<Func<TIn,TOut>> CreatePropertySelectorExpression<TIn,TOut>(string path) { Expression exp = Expression.Parameter(typeof(TIn),"x"); foreach (var property in path.Split('.')) { exp = Expression.PropertyOrField(exp,property); } return exp; } 但是……我得到了无效的施法错误!
我对表达式很新,我不知道如何继续:( 解决方法
你的exp只包含lambda的主体.但是你想要一个实际的lambda函数,它接受一个类型为TIn的参数.所以你需要使用Expression.Lambda创建一个lambda:
var param = Expression.Parameter(typeof(TIn)); var body = Expression.PropertyOrField(param,propertyName); return Expression.Lambda<Func<TIn,TOut>>(body,param); 请注意,表达式并没有真正帮助你.您可能需要编译函数: private static Func<TIn,TOut> CreatePropertyAccessor<TIn,TOut> (string propertyName) { var param = Expression.Parameter(typeof(TIn)); var body = Expression.PropertyOrField(param,propertyName); return Expression.Lambda<Func<TIn,param).Compile(); } 然后你可以像这样使用它: var name1 = CreatePropertyAccessor<Obj,string>("Name"); var name2 = CreatePropertyAccessor<Obj,string>("Name2"); var name3 = CreatePropertyAccessor<Obj,string>("Name3"); var o = new Obj() // Obj is a type with those three properties { Name = "foo",Name2 = "bar",Name3 = "baz" }; Console.WriteLine(name1(o)); // "foo" Console.WriteLine(name2(o)); // "bar" Console.WriteLine(name3(o)); // "baz" (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |