views:

75

answers:

1

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?

+2  A: 

The point of dynamic typing is to make the member names themselves get resolved from the source code member access.

Dynamic typing is working exactly as it's meant to here - it's not designed to retrieve the value of the variable and use that as the member name - it's designed to use the member name you used in your source code (i.e. "somekey").

It sounds like you really don't need dynamic typing at all here - just use Dictionary<string,List<String>> as normal:

List<String> listOfStringsAsValue = dictionary[somekey];

EDIT: It sounds like you actually want to encapsulate a dictionary like this:

public class Foo // TODO: Come up with an appropriate name :)
{
    private readonly Dictionary<string, List<string>> dictionary =
        new Dictionary<string, List<string>>();

    public List<string> this[string key]
    {
        get
        {
            List<string> list;
            if (!dictionary.TryGetValue(key, out list))
            {
                list = new List<string>();
                dictionary[key] = list;
            }
            return list;
        }
    }
}

Then you can do:

foo["first"].Add("value 1");
foo["second"].Add("value 2")
foo["first"].Add("value 1.1");

If you want to be able to attempt to fetch a list without creating a new one if it doesn't exist, you could add a method to do that.

It really doesn't sound like you need DynamicObject here.

Jon Skeet
might not used then dynamic dictionary correctly but above normal usage of dictionary won't work in my case.My keys are populating randomly and with out any sequence values of those keys that are actually the strings (those actually will be added in the list of that key) are adding. In dynamic dictionary we just refer key and it gives back values(i.e list of strings) and if I add string inside list , it actually gets added under list of that mentioned and specified key of whom values to be populated dynamically.
Usman
@Usman: That sounds entirely doable with a normal dictionary. Editing...
Jon Skeet
Bundle of Thanks Jon..It got solved.
Usman