Sam Holder
Sam Holder

Reputation: 32946

using Expression<Func<object>> as key in a dictionary

How can I use a type of Expression> as a key in a dictionary?

I have just started to play with Expression instances and am not sure if what I want to do is possible.

It seems that 2 identical Expressions are not equal as when I try I can put an entry into the dictionary using the expression as the key but it return false when I ask the dictionary if it contains the key, unless I use the very same expression instance.

TypeToTest test = new TypeToTest();
Expression<Func<object>> expression = ()=>test.PropertyA;
IDictionary<Expression<Func<object>>,bool> dictionary = new Dictionary<Expression<Func<object>>, bool> ();
dictionary[expression] = true;
Assert.That (dictionary.ContainsKey(expression), Is.True);
Assert.That (dictionary.ContainsKey(()=>test.PropertyA), Is.True);

the last line above fails, when I would like it to succeed.

The intent is to have the ability to define a set of rules which apply to properties or methods of an object so I can determine, for example, if a property is editable or if a value with a particular key in a dictionary can be deleted. I don't want to have a flag on the object which determines if it is editable (as the editablity may be different for different properties) and another which determines if it is deleteable but rather another class which will be responsible for maintaining the rules associated with an object so that as the object is extended in the future more rules may be added to describe the editablity/accessibility/deletability/ of the constituent parts of the object. if that makes sense.

There was similar post here, but this seems to key based on the property name where as, I would like that it be possible to key on the method and arguments as well, so I could support determining the editablity of objects held in a dictionary based on the key.

Would something like this be possible or is it pie in the sky?

Upvotes: 3

Views: 1058

Answers (2)

Marc Gravell
Marc Gravell

Reputation: 1063569

This an expected behaviour when comparing different references, where that type doesn't override Equals or implement IEquatable<T>. You could write a custom comparer (perhaps simply comparing the ToString()) and pass that into the dictionary - but IMO an Expression is not a good choice of key.

The following is not necessarily a robust usage via ToString() ; use at own discretion:

class Program {
    static void Main() {
        TypeToTest test = new TypeToTest();
        Expression<Func<object>> expression = () => test.PropertyA;
        IDictionary<Expression<Func<object>>, bool> dictionary =
            new Dictionary<Expression<Func<object>>, bool>(
               new ToStringComparer<Expression<Func<object>>>());
        dictionary[expression] = true;

        bool x = dictionary.ContainsKey(expression), // true
            y = dictionary.ContainsKey(() => test.PropertyA); // true
    }
}
class ToStringComparer<T> : IEqualityComparer<T> where T : class {
    public bool Equals(T x, T y) {
        if ((x == null && y == null) || ReferenceEquals(x,y)) return true;
        if (x == null || y == null) return false;
        return x.ToString() == y.ToString();
    }
    public int GetHashCode(T obj) {
        return obj == null ? 0 : obj.ToString().GetHashCode();
    }
}

Upvotes: 4

Tim Robinson
Tim Robinson

Reputation: 54764

The subclasses of the Expression type don't override the Equals and GetHashCode methods, making them difficult to use as dictionary keys. Your dictionary is using reference equality, and the two occurrences of ()=>test.PropertyA produce two different expression tree objects.

You could write your own IEqualityComparer<Expression> implementation, and pass it into the dictionary constructor. In your comparer you would write Equals and GetHashCode methods that handled each of the Expression classes and compared their properties.

Upvotes: 2

Related Questions