C# 表达式树:对象数组到 Expression.New() 参数

C# Expression tree: object array to Expression.New() parameters

我想使用表达式树来提高某些对象关系映射代码的性能。旧代码看起来像这样:

public List<T> SqlToResults<T>(string query)
{
    // Do SQL stuff, get matching constructor for type T ...
    // ...

    List<T> results = new List<T>();

    // Create buffer for constructor parameters
    object[] constructorParams = new object[reader.FieldCount];
    
    while (reader.Read())
    {
        for (int i = 0; i < reader.FieldCount; i++)
        {
            // simplefied (there'd be some mapping to match correct order of parameters)
            constructorParams[i] = reader[i];
        }
        
        // create new instance of T with our data
        T result = (T)constructorInfoOfT.Invoke(constructorParams);
        
        // add new result to list of results
        results.Add(result);
    }
    return results;
}

上面代码中的性能瓶颈是对 ConstructorInfo.Invoke() 的调用,我想将其替换为表达式树和对 Expression.New() 的调用,类似于 [=21= 中的代码].但是在编译时我不知道参数的数量和它们的类型似乎有点复杂。 Expression.New()Expressions 数组作为构造函数的参数,但我只有一个对象数组(这将是单个 ParameterExpression)。所以我不得不以某种方式遍历 ParameterExpression 的内容,然后将每个元素映射到它自己的 Expression,然后可以将其作为 Expression[] 传递给 Expression.New()

我想到的代码如下所示:

internal delegate TInstance Constructor<TInstance>(object[] parameters);

internal Constructor<T> BuildConstructorFrom<T>(ConstructorInfo constructorInfo)
{
    ParameterExpression constructorParameters = Expression.Parameter(typeof(object[]));

    Expression[] parameterExpressions;

    // ???
    // somehow map entries in constructorParameters to entries in parameterExpressions 
    // ???

    NewExpression constructorCall = Expression.New(constructorInfo, parameterExpressions);

    Constructor<T> ctor = (Constructor<T>)Expression.Lambda<Constructor<T>>(constructorCall, constructorParameters).Compile();
    return ctor;
}

我已经看过 foreach loop using expression trees and issue while building dynamic expression tree 等类似问题,但我仍然不确定如何在我的用例中使用这些循环。

我设法弄明白了。解决方案是使用 Expression.ArrayIndex 和传统的 for 循环:

internal Constructor<T> BuildConstructerFrom<T>(ConstructorInfo constructorInfo)
{
    ParameterExpression constructorParameters = Expression.Parameter(typeof(object?[]));

    ParameterInfo[] parametersInfos = constructorInfo.GetParameters();

    Expression[] parameterExpressions = new Expression[parametersInfos.Length];
    for (int i = 0; i < parametersInfos.Length; i++)
    {
        ConstantExpression ithIndex = Expression.Constant(i);
        BinaryExpression ithParameter = Expression.ArrayIndex(constructorParameters, ithIndex);
        UnaryExpression unboxedIthParameter = Expression.Convert(ithParameter, parametersInfos[i].ParameterType);
        parameterExpressions[i] = unboxedIthParameter;
    }

    NewExpression constructorCall = Expression.New(constructorInfo, parameterExpressions);

    Constructor<T> ctor = (Constructor<T>)Expression.Lambda<Constructor<T>>(constructorCall, constructorParameters).Compile();
    return ctor;
}