0

I have some code which uses a Dictionary<string, object> and I need to be able to update the Value properties inside this dictionary with a linq query like so: _properties.Where(dicEntry => dicEntry.Key.ToLower() == binder.Name.ToLower()).First().Value = value; The problem is that the Value property here is ready only. It seems there's only access to setting this Value through the indexer like so _properties["someKey"] = obj Could I somehow expose this setter through reflection or something?

EDIT: I realize this can be done without reflection etc, and that there's many simple ways to do this and that yes, reflection is not generally considered ideal in most solutions, BUT I'm mainly interested in how you would do what I'm asking for in this situation. I.e. How can I make Value not read-only, make it mutable. (overrides or reflection or some other means?)

Here's the below code how I want to use it in a method.

public override bool TrySetMember(SetMemberBinder binder, object value)
{
    if (_properties.ContainsKey(binder.Name))
    {
       //here's the old way
       //_properties[binder.Name] = value;
       //but i'd like to use the below non-compilable code to allow case insensitivity
        _properties.Where(dicEntry => dicEntry.Key.ToLower() == binder.Name.ToLower()).First().Value = value;
        return true;
    }
    else
    {
        return false;
    }
}
Michael Socha
  • 1,748
  • 1
  • 16
  • 17
  • 1
    Do you *always* want your dictionary to be case-insensitive? If so, just create it that way, passing in an appropriate `StringComparer`. – Jon Skeet Oct 24 '14 at 19:25
  • I can't believe I got a comment from the great Jon Skeet! But yes, you're right, that's all I need to do. I didn't realize it was that easy because I guess I'm still curious about how you could do it with reflection with some kind of hack-ish method to set Value as a sett-able property. I've done something like this in the past exposing private members with reflection, but I'm interested in how different people approach this as well. I guess I'm interested more in delving into the inner workings of C# a little and simultaneously learning about the preferences and styles of different peeps. – Michael Socha Oct 24 '14 at 21:03
  • Reflection is generally a tool of last resort. See http://stackoverflow.com/q/429962/397817 – Stephen Kennedy Oct 25 '14 at 11:11
  • I am aware of this general presupposition. – Michael Socha Oct 25 '14 at 18:49

1 Answers1

1

If you want to achieve case-insensivity, then here is the way to go:

var caseInsensitiveDictionary = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);

Or similar to what you are trying to achieve:

var dict = new Dictionary<string, string>();
            dict.Add("test", "blah");
            dict.Add("tesT", "meh");

            dict[dict.Keys.Where(k => k.Equals("test", StringComparison.OrdinalIgnoreCase)).First()] = "this";
vmg
  • 9,920
  • 13
  • 61
  • 90
  • Ahh yes, that is interesting. Wouldn't have thought of that. That's interesting and would be the answer closest so far to what I am searching for. Would still love to see what could be done with reflection though, not because I need to but because I could use the mechanisms learned through such an answer for many unforeseeable uses. – Michael Socha Oct 25 '14 at 18:43