编程语言
首页 > 编程语言> > c#-记忆表达式的最快方法

c#-记忆表达式的最快方法

作者:互联网

我有一个函数,将输入Expression转换为Output BlockExpression.所以我写这段代码:

    private static readonly Dictionary<Expression, BlockExpression> MemberMemoizeDictionary = new Dictionary<Expression, BlockExpression>(); 
    private static BlockExpression CreateBody<TProperty>(CustomComparer<T> comparer, Expression<Func<T, TProperty>> member, bool createLabel)
        where TProperty : IComparable<TProperty>, IComparable
    {
        BlockExpression expression;
        if (MemberMemoizeDictionary.TryGetValue(member, out expression))
        {
            return expression;
        }

        MemberExpression memberExpression = (MemberExpression) (member.Body is MemberExpression ? member.Body : ((UnaryExpression)member.Body).Operand);
        BlockExpression result = comparer.CreateCompareTo<TProperty>(memberExpression, createLabel);
        MemberMemoizeDictionary[member] = result;
        return result;
    }

但它不起作用.

我当时以为表达式是不可变的,因此我可以将它们用作字典键,但事实并非如此.

解决此问题的最简单,最快的方法是什么?它始终是单个成员表达式,由于值类型属性的装箱而可能进行转换.

解决方法:

正如xantos所说,表达式树是引用相等的,因此您不能将它们用作字典键.使用MemberInfo作为您的密钥,它将起作用.

private static readonly Dictionary<MemberInfo, BlockExpression> MemberMemoizeDictionary = new Dictionary<MemberInfo, BlockExpression>(); 
private static BlockExpression CreateBody<TProperty>(CustomComparer<T> comparer, Expression<Func<T, TProperty>> member, bool createLabel)
    where TProperty : IComparable<TProperty>, IComparable
{
    BlockExpression expression;
    MemberExpression memberExpression = (MemberExpression) (member.Body is MemberExpression ? member.Body : ((UnaryExpression)member.Body).Operand);
    if (MemberMemoizeDictionary.TryGetValue(memberExpression.Member, out expression))
    {
        return expression;
    }

    BlockExpression result = comparer.CreateCompareTo<TProperty>(memberExpression, createLabel);
    MemberMemoizeDictionary[member] = result;
    return result;
}

免责声明:我没有检查此代码是否可以编译,但是我想您明白了:)

标签:memoization,expression,c,net,expression-trees
来源: https://codeday.me/bug/20191120/2043593.html