从字符串创建属性选择器表达式

Sup*_*JMN 7 c# linq reflection lambda

我正在尝试从字符串生成"属性选择器".

让我用一个真实的例子来解释一下自己:

我们有一个Person类,其中包含Name(string)属性.

我可以像这个propertySelector一样手动创建一个"属性选择器":

Expression<Func<Person, string>> propertySelector = x => x.Name;
Run Code Online (Sandbox Code Playgroud)

但我想用我的方法获得相同的属性选择器.

var propertySelector = CreatePropertySelectorExpression<Person, string>("Name");
Run Code Online (Sandbox Code Playgroud)

到目前为止我所拥有的是:

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;
}
Run Code Online (Sandbox Code Playgroud)

但是......我得到了无效的施法错误!

无法将类型'System.Linq.Expressions.Expression'隐式转换为'System.Linq.Expressions.Expression>'.存在显式转换(您是否错过了演员?)

我对表达式很新,我不知道如何继续:(

pok*_*oke 16

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);
Run Code Online (Sandbox Code Playgroud)

请注意,表达式并没有真正帮助你.您可能需要编译函数:

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, TOut>>(body, param).Compile();
}
Run Code Online (Sandbox Code Playgroud)

然后你可以像这样使用它:

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"
Run Code Online (Sandbox Code Playgroud)