如何使用方法调用生成已编译的lambda? [英] How do I generate a compiled lambda with method calls?

查看:69
本文介绍了如何使用方法调用生成已编译的lambda?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行时为给定成员生成已编译的getter方法.现在,我的代码只是假设getter方法的结果是一个字符串(可以很好地用于测试).但是,我想使用我编写的自定义转换器类来实现此目的,请参见下文,添加的"ConverterBase"参考.

I'm generating compiled getter methods at runtime for a given member. Right now, my code just assumes that the result of the getter method is a string (worked good for testing). However, I'd like to make this work with a custom converter class I've written, see below, "ConverterBase" reference that I've added.

我不知道如何将对转换器类的调用添加到表达式树中.

I can't figure out how to add the call to the converter class to my expression tree.

    public Func<U, string> GetGetter<U>(MemberInfo info)
    {
        Type t = null;
        if (info is PropertyInfo) 
        {
            t = ((PropertyInfo)info).PropertyType;
        }
        else if (info is FieldInfo)
        {
            t = ((FieldInfo)info).FieldType;
        }
        else
        {
            throw new Exception("Unknown member type");
        }

        //TODO, replace with ability to specify in custom attribute
        ConverterBase typeConverter = new ConverterBase();

        ParameterExpression target = Expression.Parameter(typeof(U), "target");
        MemberExpression memberAccess = Expression.MakeMemberAccess(target, info);

        //TODO here, make the expression call "typeConverter.FieldToString(fieldValue)"

        LambdaExpression getter = Expression.Lambda(memberAccess, target);

        return (Func<U, string>)getter.Compile();
    }

我正在寻找要放入第二个TODO区域的内容(我可以处理第一个:)).

I'm looking for what to put in the second TODO area (I can handle the first :)).

生成的已编译lambda应将类型为U的实例作为参数,调用指定的成员访问函数,然后使用结果调用转换器的"FieldToString"方法,并返回生成的字符串.

The resulting compiled lambda should take an instance of type U as a param, call the specified member access function, then call the converter's "FieldToString" method with the result, and return the resulting string.

推荐答案

您能说明一下表达式要计算的内容(如果是常规C#)?我可以很容易地写出表达式-我只是不完全理解这个问题...

Can you illustrate what (if it was regular C#) you want the expression to evaluate? I can write the expression easily enough - I just don't fully understand the question...

(编辑评论)-在这种情况下,它将类似于:

(edit re comment) - in that case, it'll be something like:

    ConverterBase typeConverter = new ConverterBase();
    var target = Expression.Parameter(typeof(U), "target");
    var getter = Expression.MakeMemberAccess(target, info);
    var converter = Expression.Constant(typeConverter, typeof(ConverterBase));

    return Expression.Lambda<Func<U, string>>(
    Expression.Call(converter, typeof(ConverterBase).GetMethod("FieldToString"),
        getter), target).Compile();

或者如果类型拒绝绑定,则需要注入强制转换/转换:

Or if the type refuses to bind, you'll need to inject a cast/convert:

    MethodInfo method = typeof(ConverterBase).GetMethod("FieldToString");
    return Expression.Lambda<Func<U, string>>(
        Expression.Call(converter, method,
            Expression.Convert(getter, method.GetParameters().Single().ParameterType)),
            target).Compile();

这篇关于如何使用方法调用生成已编译的lambda?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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