动态词典的使用在C# [英] Dynamic Dictionary usage in C#

查看:121
本文介绍了动态词典的使用在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是动态词典在C#。我现在面临的问题是TryGetMember哪个我重写在动态辞典类的行为。

I am using a Dynamic dictionary in C#. The problem I am facing is the behavior of TryGetMember which I am overriding in the dynamic dictionary class.

下面是动态辞典的code。

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

下面,TryGetMember将在运行时调用时,我们参考了一些关键的外部,但它的奇怪的是粘合剂的姓名部件,它总是给人关键是我们从外面是指,它始终解析写为字母的字​​符键名。

Here, TryGetMember will be called at runtime whenever we refer some key from outside, but it's strange that binder's Name member which always gives the key what we refer from outside, it always resolves the key name written as characters of alphabets.

例如。如果DynamicDictionary的对象所做的:

e.g. if the object of DynamicDictionary made 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 

现在这里发生的事情是somekey将成为a_binder(即a_binder.Name =somekey)的值。它应被解析为a_binder.Name =测试,然后从动态辞典它将定位listOfStrings针对此键(即实际的测试,但它解决不是值但实际的变量名作为关键字)。

Now what happens here is "somekey" will become the value of a_binder (i.e a_binder.Name="somekey"). It should be resolved as a_binder.Name = "Test" and then from the 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).

有没有解决的办法吗?

推荐答案

动态类型的要点是使成员名称本身得到从源头code成员访问解决。

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

动态类型的工作,正是因为它的意思在这里 - 它不是的设计的检索变量的值,并用其作为成员的名字 - 它的设计使用您所使用的成员名称您来源$ C ​​$ C(即somekey)。

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").

这听起来像你真的不需要动态类型都在这里 - 只需使用词典&LT;字符串列表&LT;字符串&GT;&GT; 正常:

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

编辑:这听起来像你真的想封装一个像这样的字典:

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.

这真的不喜欢你的声音在这里需要DynamicObject。

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

这篇关于动态词典的使用在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆