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