.NET:开关VS字典字符串键字符串、字典、NET、VS

2023-09-04 02:09:13 作者:乳来伸掌

我有一个情况我有不同类型的大约15属性的业务对象。业务对象还具有实现一个接口,它具有以下的方法:

 对象使用getFieldValue(字符串字段名);
 

我可以看到2种方式实现此方法:

使用switch语句:

 开关(字段名)
{
    案字段1:返回this.Field1;
    案字段2:返回this.Field2;
    // 等等。
}
 
Python学习笔记之 高级变量类型 列表,元组,字典,字符串的相关常用 操作

使用字典(SortedDictionary或哈希表):

 返回this.AllFields [字段名]。
 

这将是更有效率?

补充:忘了说。这种方法是用于显示在一个网格中的项目。网格将有对每个这些属性的一列。这里将定期为网格带着几分超过1000个项目在其中。这就是为什么我很担心性能。

新增2:

下面有一个想法:一种混合方法。做一个静态字典的键是属性名称和值是在数组索引。词典填充只有一次,在应用程序的启动。每个对象实例都有一个数组。因此,查找会是这样的:

 返回this.ValueArray [StaticDictionary [字段名];
 

这本字典填充算法可以使用​​反射。属性本身将被相应地实施:

 公共BOOL字段1
{
    得到
    {
        对象o = this.ValueArray [StaticDictionary [字段1]];
        回复O == NULL?假:(布尔)O;
    }
    组
    {
        this.ValueArray [StaticDictionary [字段1] =值;
    }
}
 

任何人都可以看到这个什么问题?

它也可以采取更进一步的ValueArray / StaticDictionary可以被放置在一个单独的泛型类型 ValueCollection< T> ,其中 T 将指定反射类型。 ValueCollection也将处理这种情况时,没有值已设置呢。属性然后可以简单地写为:

 公共BOOL字段1
{
    得到
    {
        返程(布尔)this.Values​​ [字段1];
    }
    组
    {
        this.Values​​ [字段1] =价值;
    }
}
 

而在最后,我又开始想,如果一个简单的switch语句可能不是速度更快,更易于维护......

解决方案

 开关:高效率,少维护
词典:良好的效率,更好的可维护性
反思:最高效,最可维护性
 

提示:忽略效率,并担心可维护性,除非您已经实际测试性能,并发现它是一个问题。

我并不是说反思是你不二的选择,只是因为这会允许你添加/删除,并根据需要重新命名的属性,而不需要保持同步的switch语句或字典。

I've got a situation where I have a business object with about 15 properties of different types. The business object also has to implement an interface which has the following method:

object GetFieldValue(string FieldName);

I can see 2 ways of implementing this method:

Use a switch statement:

switch ( FieldName )
{
    case "Field1": return this.Field1;
    case "Field2": return this.Field2;
    // etc.
}

Use a dictionary (SortedDictionary or HashTable?):

return this.AllFields[FieldName];

Which would be more efficient?

Added: Forgot to say. This method is for displaying the item in a grid. The grid will have a column for each of these properties. There will routinely be grids with a bit over 1000 items in them. That's why I'm concerned about performance.

Added 2:

Here's an idea: a hybrid approach. Make a static dictionary with keys being property names and values being indices in array. The dictionary is filled only once, at the startup of the application. Every object instance has an array. So, the lookup would be like:

return this.ValueArray[StaticDictionary[FieldName]];

The dictionary filling algorithm can use reflection. The properties itself will then be implemented accordingly:

public bool Field1
{
    get
    {
        object o = this.ValueArray[StaticDictionary["Field1"]]; 
        return o == null ? false : (bool)o;
    }
    set
    {
        this.ValueArray[StaticDictionary["Field1"]] = value;
    }
}

Can anyone see any problems with this?

It can also be taken one step further and the ValueArray/StaticDictionary can be placed in a separate generic type ValueCollection<T>, where T would specify the type for reflection. ValueCollection will also handle the case when no value has been set yet. Properties could then be written simply as:

public bool Field1
{
    get
    {
        return (bool)this.Values["Field1"];
    }
    set
    {
        this.Values["Field1"] = value;
    }
}

And in the end, I'm starting to wonder again, if a simple switch statement might not be both faster and easier to maintain....

解决方案

switch:      good efficiency, least maintainable
dictionary:  good efficiency, better maintainability
reflection:  least efficient, best maintainability

Hint: ignore efficiency and worry about maintainability only, unless you've actually tested performance and found it be be an issue.

I'm not saying reflection is your only choice, just that it would allow you to add/remove and rename properties as needed, and not need to keep the switch statement or dictionary in sync.