创建表达式< Func< T,对象>>从给定的类型 [英] Create Expression<Func<T, object>> from a given Type

查看:116
本文介绍了创建表达式< Func< T,对象>>从给定的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试传递这些表达式的方法具有以下签名:

  public virtual CsvPropertyMap< TClass,TProperty>地图< TProperty>(表达< Func< TClass,TProperty>>表达式)
{
//
}

所以你通常会把它称为,你想要映射的任何给定类型,像这样(对于具有名为stringProperty的属性的类型):

  mapper.Map(x => x.StringProperty);传递一个内部转换成一个表达式< Func< T中的lambda的



,对象>>



我已尝试使用表达式在代码中创建此表达式。在编译时,它一切正常(因为它返回一个 Expression< Func< TModel,object>> ),但在运行时我收到异常'不是成员访问。以下是代表我要映射的属性的PropertyInfo对象的代码:

  private Expression&FunC< TModel,object> > CreateGetterExpression(PropertyInfo propertyInfo)
{
var getter = propertyInfo.GetGetMethod();

表达式< Func< TModel,object>> expression = m => getter.Invoke(m,new object [] {});
返回表达式;
}

基本上,如何在代码中正确构建Expression?

解决方案

只需尝试看起来像这样:

  public static Expression< Func< T,P>> GetGetter< T,P>(string propName)
{
var parameter = Expression.Parameter(typeof(T));
var property = Expression.Property(parameter,propName);
return Expression.Lambda< Func< T,P>>(property,parameter);
}

public static Expression< Func< T,P>> GetGetter< T,P>(PropertyInfo propInfo)
{
var parameter = Expression.Parameter(typeof(T));
var property = Expression.Property(parameter,propInfo);
return Expression.Lambda< Func< T,P>>(property,parameter);
}

这是用法的例子:

 私人类TestCalss 
{
public int Id {get;组;
}

private static void Main(string [] args)
{
var getter = GetGetter&TestCalss,int>(typeof(TestCalss).GetProperty ID))编译();
Console.WriteLine(getter(new TestCalss {Id = 16}));
}


I am looking to use CsvHelper dynamically by building up Expressions in code which represent property member access for a given type.

The method I am trying to pass these expressions to has the following signature:

    public virtual CsvPropertyMap<TClass, TProperty> Map<TProperty>( Expression<Func<TClass, TProperty>> expression )
    {
        //
    }

So you would normally call it, for any given type you want to map, like this (for a type with a property called 'stringProperty'):

mapper.Map(x => x.StringProperty);

Passing in a lambda which is converted internally into an Expression<Func<T, object>>

I have tried to create this expression in code, using Expressions. At compile time it all works fine (in that it returns an Expression<Func<TModel, object>>), but at runtime I get an exception 'not a member access'. Here is the code which takes a PropertyInfo object representing the property I want to map:

    private Expression<Func<TModel, object>> CreateGetterExpression( PropertyInfo propertyInfo )
    {
        var getter = propertyInfo.GetGetMethod();

        Expression<Func<TModel, object>> expression = m => getter.Invoke( m, new object[] { } );
        return expression;
    }

Basically, how do I build that Expression up properly in code?

解决方案

Just try something looks like this:

    public static Expression<Func<T, P>> GetGetter<T, P>(string propName)
    {
        var parameter = Expression.Parameter(typeof(T));
        var property = Expression.Property(parameter, propName);
        return Expression.Lambda<Func<T, P>>(property, parameter);
    }

    public static Expression<Func<T, P>> GetGetter<T, P>(PropertyInfo propInfo)
    {
        var parameter = Expression.Parameter(typeof(T));
        var property = Expression.Property(parameter, propInfo);
        return Expression.Lambda<Func<T, P>>(property, parameter);
    }

It's example of usages:

    private class TestCalss
    {
        public int Id { get; set; }
    }

    private static void Main(string[] args)
    {
        var getter = GetGetter<TestCalss, int>(typeof(TestCalss).GetProperty("Id")).Compile();
        Console.WriteLine(getter(new TestCalss { Id = 16 }));
    }

这篇关于创建表达式&lt; Func&lt; T,对象&gt;&gt;从给定的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆