C# 访问常量键值数据的有效方法

C# 访问常量键值数据的有效方法,c#,dictionary,keyvaluepair,C#,Dictionary,Keyvaluepair,我想存储键值数据,并能够以高效的方式访问它 基本上:我有一个自定义对象(EquipmentObj),该对象中有一个名为“DeviceType”的属性。在对象的构造函数中,我传递一个字符串,该字符串输出到字典(EquipmentObj的局部变量),如果字典有键,则返回一个值 为了尽量减少在堆上初始化字典25次(EquipmentObj被实例化25-50次),我想知道是否有更有效的方法来实现这一点 我的第一个想法是XML,但我不能添加反序列化;我不会卷入这件事的 我的下一个想法可能是使用静态类。但是

我想存储键值数据,并能够以高效的方式访问它

基本上:我有一个自定义对象(EquipmentObj),该对象中有一个名为“DeviceType”的属性。在对象的构造函数中,我传递一个字符串,该字符串输出到字典(EquipmentObj的局部变量),如果字典有键,则返回一个值

为了尽量减少在堆上初始化字典25次(EquipmentObj被实例化25-50次),我想知道是否有更有效的方法来实现这一点

我的第一个想法是XML,但我不能添加反序列化;我不会卷入这件事的

我的下一个想法可能是使用静态类。但是我仍然需要定义KeyValuePair或Dictionary,静态类不能有实例成员

你们都有什么建议

这是我现在基本上正在做的一个例子

class EquipmentObj
    {
        public EquipmentObj(string deviceType)
        {
            addItems(); 
            this.DeviceType = EquipmentList.ContainsKey(device_Type) ? EquipmentList[deviceType] : "Default";
        }
        public string DeviceType { get; set; }
        private Dictionary<string, string> EquipmentList = new Dictionary<string, string>();

        private void addItems()
        {
            //Add items to Dictionary 
        }
    }
class EquipmentObj
{
公共设备OBJ(字符串设备类型)
{
addItems();
this.DeviceType=EquipmentList.ContainsKey(设备类型)?EquipmentList[DeviceType]:“默认”;
}
公共字符串设备类型{get;set;}
专用字典设备列表=新字典();
私有void附加项()
{
//将项目添加到字典
}
}

A
静态
类不能有实例成员,但非静态类可以有
静态
成员。您可以创建
EquipmentList
addItems()
这两个
静态
,而无需更改
EquipmentObj
本身

class EquipmentObj
{
    public EquipmentObj(string deviceType)
    {
        addItems(); 
        this.DeviceType = EquipmentList.ContainsKey(device_Type) ? EquipmentList[deviceType] : "Default";
    }
    public string DeviceType { get; set; }
    private static Dictionary<string, string> EquipmentList = new Dictionary<string, string>();

    public static void addItems()
    {
        //Add items to Dictionary 
    }
}

您可以创建一个管理器类来处理设备类型,这不是必需的,但它确实分离了逻辑,并使从应用程序中的其他区域访问设备类型变得更容易

public class EquipmentObj
{
    public EquipmentObj(string deviceType)
    {
        this.DeviceType = EquipmentObjManager.GetDeviceType(deviceType);
    }
    public string DeviceType { get; set; }
}

public class EquipmentObjManager
{
    private static Dictionary<string, string> EquipmentList = new Dictionary<string, string>();

    public static string GetDeviceType(string deviceType)
    {
        return EquipmentList.ContainsKey(deviceType) ? EquipmentList[deviceType] : "Default";
    }

    public static void AddDeviceType(string deviceTypeKey, string deviceType)
    {
        if (!EquipmentList.ContainsKey(deviceTypeKey))
        {
            EquipmentList.Add(deviceTypeKey, deviceType);
        }
    }
}
公共级设备OBJ
{
公共设备OBJ(字符串设备类型)
{
this.DeviceType=EquipmentObjManager.GetDeviceType(DeviceType);
}
公共字符串设备类型{get;set;}
}
公共级设备管理器
{
私有静态字典设备列表=新字典();
公共静态字符串GetDeviceType(字符串deviceType)
{
返回设备列表.ContainsKey(设备类型)?设备列表[设备类型]:“默认”;
}
公共静态void AddDeviceType(string deviceType键,string deviceType)
{
如果(!EquipmentList.ContainsKey(设备类型键))
{
设备列表。添加(设备类型键,设备类型);
}
}
}
我不确定您在词典中的位置,但您可以调用
EquipmentObjManager.AddDeviceType
将项目添加到词典中


可能不是您的最佳解决方案,但它可能会有所帮助:)

首先如何填充字典?静态类可以有静态成员。但是你不需要,为什么不让EquipmentList成为EquipmentObj的静态成员呢?这听起来很像微观优化——在它被证明是一个问题之前,不要浪费时间,这是我的建议。@Casperah——通常,我同意,仅仅为了性能问题,不值得这么做。但是最好的设计是首先将字典
设置为静态的
,因此值得这样做。设备列表字典是否表示设备Obj中包含的设备?或者这是另外一回事?我正在填充字典,只是遗漏了这些条目,因为它们是业务敏感的。
public class EquipmentObj
{
    public EquipmentObj(string deviceType)
    {
        this.DeviceType = EquipmentObjManager.GetDeviceType(deviceType);
    }
    public string DeviceType { get; set; }
}

public class EquipmentObjManager
{
    private static Dictionary<string, string> EquipmentList = new Dictionary<string, string>();

    public static string GetDeviceType(string deviceType)
    {
        return EquipmentList.ContainsKey(deviceType) ? EquipmentList[deviceType] : "Default";
    }

    public static void AddDeviceType(string deviceTypeKey, string deviceType)
    {
        if (!EquipmentList.ContainsKey(deviceTypeKey))
        {
            EquipmentList.Add(deviceTypeKey, deviceType);
        }
    }
}