I am using Dynamic dictionary in C#. The problem which I am facing , is the usage of TryGetMember behavior which i am overriding in dynamic ditionary class.
here's the code of dynamic dictionary.
class DynamicDictionary<TValue> : DynamicObject
{
private IDictionary<string, TValue> m_dictionary;
public DynamicDictionary(IDictionary<string, TValue> a_dictionary)
{
m_dictionary = a_dictionary;
}
public override bool TryGetMember(GetMemberBinder a_binder, out object a_result)
{
bool returnValue = false;
var key = a_binder.Name;
if (m_dictionary.ContainsKey(key))
{
a_result = m_dictionary[key];
returnValue = true;
}
else
a_result = null;
return returnValue;
}
}
Here TryGetMember will be called at runtime whenever we refer some key from outside. But strange here that binder's Name member which always give the key what we refer from outside. Strange here that it always resolve the key name what written as characters of alphabets.
e.g if make the object of DynamicDictionary as
Dictionary<string,List<String>> dictionaryOfStringVsListOfStrings;
//here listOfStrings some strings list already populated with strings
dictionaryOfStringVsListOfStrings.Add("Test", listOfStrings);
dynamic dynamicDictionary_01 = new
DynamicDictionary<List<String>(dictionaryOfStringVsListOfStrings);
string somekey = "Test";
//will be resolve at runtime
List<String> listOfStringsAsValue = dynamicDictionary_01.somekey
Now here what happens actually "somekey" will become the value of a_binder (i.e a_binder.Name="somekey") what I need that key assigned value should be resolved as key. i.e it should be resolved as a_binder.Name = "Test" and then from dynamic dictionary it will locate listOfStrings against this key i.e actually "Test" but it resolves not the value but actual variable name as key..
Way around?