简体   繁体   English

在 C# 中的字典中存储类似对象的最佳方法是什么?

[英]What is the best way to store similar objects in a dictionary in C#?

I have several "objects" all having the same fields, let's say :我有几个“对象”都具有相同的字段,让我们说:

public string Reference;
public int Id;
public Dictionary<string, string> Members;

They will all be initialized once at launch so they might be static BUT I will have to read their fields this way :它们都将在启动时初始化一次,因此它们可能是static但我必须以这种方式读取它们的字段:

Dictionary<string, "object"> MyTypes;
//all my objects are store in this varaible (either by value or reference)

string thisMember = MyTypes[firstKey].Members[secondKey];

knowing fristKey and secondKey , but without having other details about the "object" precise type.知道fristKeysecondKey ,但没有关于“对象”精确类型的其他详细信息。

How should I declare this "object" (should it be a class, a struct, should it have const fields...) to do it the "right" way, and the most CPU-friendly way (lot of calls needs to be issued as fast as possible) ?我应该如何声明这个“对象”(它应该是一个类,一个结构,它是否应该有 const 字段......)以“正确”的方式,以及最 CPU 友好的方式(需要大量调用)尽快发出)?

I'm open to any solution allowing me to keep those objects distincts (if possible), and allowing me store them in a Dictionary (this can't be changed).我对任何允许我保持这些对象不同(如果可能),并允许我将它们存储在Dictionary解决方案持开放态度(这不能改变)。

I tried having those objects static but as static objects can't inherit nor implement interfaces I can't have my common "object" for my dictionary MyTypes .我尝试将这些对象设为静态,但由于static对象无法继承或实现接口,因此我的字典MyTypes无法拥有通用的“对象”。 I also tried with interfaced struct, but I can't use a default constructor or directly initialize them in the "object" declaration, it doesn't feel it's the best solution.我也尝试过接口结构,但我不能使用默认构造函数或直接在“对象”声明中初始化它们,它并不觉得它是最好的解决方案。

I have struggling with this problem for hours and I'm running out of thoughts.我已经为这个问题苦苦挣扎了几个小时,而且我已经没有想法了。 What are yours?你的是什么?

Imagine the interface like a contract that a class must respect, it can do other things but MUST implements the member of the interface把接口想象成一个类必须遵守的契约,它可以做其他事情但必须实现接口的成员

you can define an interface like this你可以定义一个这样的接口

public interface IReadOnlyNiceInterface
{
    string Reference {get;}
    int Id {get;}
    Dictionary<string, string> Members {get;}
}

so your interface is read only, implementation and instantiation is then completely independent所以你的接口是只读的,实现和实例化是完全独立的

your dictionary will be like你的字典会像

Dictionary<string, IReadOnlyNiceInterface> MyTypes;

and your types will be like你的类型会像

public class Type1Imple : IReadOnlyNiceInterface
{
    //implements member
}

I have several "objects" all having the same fields我有几个“对象”都具有相同的字段

That sounds like a candidate for a base class or an interface, from which all other special kinds of that class derive.这听起来像是基类或接口的候选者,该类的所有其他特殊类型都从它派生而来。 Give that base class a name, and use the base classes' name in your dictionary definition:为该基类命名,并在字典定义中使用基类的名称:

Dictionary<string, BaseClass> MyTypes;

public class BaseClass
{
    public string Reference {get; set;}
    public int Id {get; set;}
    public Dictionary<string, string> Members {get; set;}
}

public class SpecialClass : BaseClass
{
    // you can add an instance of this class to your dictionary too!
}

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

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