我正在尝试从字符串生成“属性选择器”.
让我用一个真实的例子来解释一下自己:
我们有一个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;
- }
但是……我得到了无效的施法错误!
Cannot implicitly convert type ‘System.Linq.Expressions.Expression’ to
‘System.Linq.Expressions.Expression>’. An
explicit conversion exists (are you missing a cast?)
我对表达式很新,我不知道如何继续:(
解决方法
你的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"