Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3593916
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T19:38:28+00:00 2026-05-18T19:38:28+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T19:38:29+00:00Added an answer on May 18, 2026 at 7:38 pm

    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();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can't use TryGetValue() from dictionary in linq expression with anonymous type. Dictionary<string, string>
You can use App.config; but it only supports key/value pairs. You can use .Net
What regular expression can I use (if any) to validate that a given string
In C# 3.0 you can use Expression to create a class with the following
Is there a list describing all of the data_type parameters you can use in
You can use more than one css class in an HTML tag in current
You can use a standard dot notation or a method call in Objective-C to
I can use properties of an Excel Worksheet to tell if the worksheet is
You can use ftplib for full FTP support in Python. However the preferred way
You can use SelectFolder() to get a folder or GetOpenFolderitem(filter as string) to get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.