记忆表情的最快方法

Fastest way to memoize expression

我有一个函数,可以将输入 Expression 转换为输出 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;
    }

但它不起作用。

我在想 Expressions 是不可变的,所以我可以将它们用作字典键,但我发现这不是真的。

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

I was thinking that Expressions are immutable

正确

但请注意,每次都会重新生成表达式!

public static Expression Exp = null;

public static void Foo(Expression<Func<bool>> exp)
{
    if (Exp == null)
    {
        Exp = exp;
    }
    else
    {
        Console.WriteLine(object.ReferenceEquals(Exp, exp));
    }

}

for (int i = 0; i < 2; i++)
{
    Foo(() => true);
}

False

遗憾的是 "literal" Expression 不是 C# 编译器 "interned"。它甚至写在 MSDN 的某处。

如 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;
}

免责声明:我没有检查这段代码是否编译,但我想你明白了:)