简体   繁体   English

类型和字典列表的替代

[英]Alternative to List of Type and dictionary

Ideally this is what I want to do. 理想情况下,这就是我想要做的。

  private List<Type,Dictionary<string,string>> dynamicFields;

example of this would be. 这样的例子就是。

  Class1      "1","value"
              "2","value1"

Why i want this for, is I will add in one go Type and some dictionary fields for each and then use linq later to retrieve those belonging to each class for some other operation. 我为什么要这样做,是我将为每个类型添加一次type和一些字典字段,然后稍后使用linq来检索属于每个类的那些字段,以进行其他一些操作。

Since this is not possible as compiler will give me error. 由于这是不可能的,因为编译器会给我错误。

"using the Generic type List requires one argument" “使用通用类型列表需要一个参数”

What could be the approach to create kind of object I want to. 创建我想要的对象的方法可能是什么。

The simplest answer would be to use dictionary of a dictionary: 最简单的答案是使用字典的字典:

private Dictionary<Type, Dictionary<string, string>> dynamicFields;

Note you do need to make sure you initialize each outer entry with a Dictionary<string, string> before adding entries, and to get an inner entry you would need to make two calls to TryGetValue . 请注意,您确实需要确保在添加条目之前用Dictionary<string, string>初始化每个外部条目,并且要获取内部条目,您需要两次调用TryGetValue As you describe it you are nesting containers, and passing the inner dictionary to another method, so this may indeed be the best option. 正如您描述的那样,您正在嵌套容器,并将内部字典传递给另一种方法,因此这确实是最好的选择。


Hopefully there is only one Dictionary<string, string> for each Type . 希望每个Type只有一个Dictionary<string, string> If not, and you cannot merge them, you would now need a list and things get one more step uglier: 如果没有,您将无法合并它们,那么您现在需要一个列表,事情变得更糟了:

private Dictionary<Type, List<Dictionary<string, string>>> dynamicFields;

However this would still arguably be the most efficient way of retrieving the list of inner dictionaries given a certain Type . 但是,可以说,这仍然是检索给定Type的内部字典列表的最有效方法。


Now, you were looking for an alternative. 现在,您正在寻找替代方案。 I am not sure if this applies in your particular case, but it sounds like you're trying to create a set of property bags, what I might want to call a static extension property. 我不确定这是否适用于您的特定情况,但是听起来您正在尝试创建一组属性包,我可能希望将其称为静态扩展属性。 I would question whether you truly want the property bags to be static, or if they would instead belong to an instance of Class1 et al. 我会问您是否真的希望属性包是静态的,或者它们是否属于Class1等的实例 In this case I would recommend instead to have your classes inherit from a base class which contains an extensible property bag. 在这种情况下,我建议您从包含可扩展属性包的基类继承您的类。 ExpandoObject basically already does this, but you could implement something yourself if you explicitly wanted a dictionary of strings. ExpandoObject基本上已经做到了这一点,但是如果您明确想要一个字符串字典,则可以自己实现。 For example: 例如:

abstract class DynamicFieldEntity
{
    protected Dictionary<string, string> DynamicFields { get; } = 
        new Dictionary<string, string>();
}

class Class1 : DynamicFieldEntity {}

Because List accepts just one argument as generic parameter. 因为List仅接受一个参数作为通用参数。 You must declare a class A like this: 您必须像这样声明A类:

public class A
{
     public Type Type { get; set; }
     public Dictionary<string, string> KeyValue { get; set; }
}

and then declare list as private List<A> dynamicFields 然后将列表声明为private List<A> dynamicFields

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM