【问题标题】:Appropriate c# collection for fast search by multy keys通过多键快速搜索的适当 c# 集合
【发布时间】:2018-02-13 07:41:30
【问题描述】:

您好,我正在重构一些遗留代码。有一些代码表示从自定义类型到 c# 类型的“转换器”。

          ...
           if (dataType == CustomType.Bit)
            {
                return typeof(bool);
            }
            else if (dataType == CustomType.Bittype ||
                dataType == CustomType.Char ||
                dataType == CustomType.Fromtotype ||
                dataType == CustomType.Mdcintervaltype ||
                dataType == CustomType.Nclob ||
                dataType == CustomType.Nchar ||
                dataType == CustomType.Ntext ||
                dataType == CustomType.Nvarchar ||
                dataType == CustomType.Nvarchar2 ||
                dataType == CustomType.Varchar ||
                dataType == CustomType.Varchar2)
            {
                return typeof(string);
            }
            else if (dataType == CustomType.Date ||
                dataType == CustomType.Datetime ||
                dataType == CustomType.Timestamp3 ||
                dataType == CustomType.Timestamp6)
            {
                return typeof(DateTime);
            }
            else if (dataType == CustomType.Decimal ||
                dataType == CustomType.Money ||
                dataType == CustomType.Number ||
                dataType == CustomType.Numeric)
            {
                return typeof(decimal);
            }

...

问:我正在寻找一些能够帮助我快速搜索一对多关系的 C# 结构。 (即我想要看起来像 { Collection possible keys} ==> { value }

P.s 我认为每个 Custromtype 都是关键并返回相同类型的简单字典并不“美丽”

new Dictionary<string, Type>()(){
    { CustomType.Bittype, typeof(string)},
    { CustomType.Fromtotype, typeof(string)}
     ...
    { CustomType.Datetime,  typeof(DateTime)},
    { CustomType.Date, typeof(DateTime)}
    ....
}

【问题讨论】:

  • 字典或开关是你的朋友。有时代码并不漂亮,不妨看看微软源代码。然而美在于优雅的设计,即以健壮、可读和可维护的方式做事
  • 你能控制那些CustomTypes 吗?如果是这样,他们可以有一个只读字段Type,它返回所需的转换类型。
  • 另一种选择是为CustomType 的每个包含匹配Type 的成员添加一个自定义属性,然后直接使用反射获取Type,或者使用字典或类似方法准备缓存结构体。 PS。我会为所述属性的存在添加一个单元测试,因此如果CustomType 成员的数量发生变化,就不会错过该属性。

标签: c# algorithm design-patterns data-structures collections


【解决方案1】:

如果您将string 作为默认类型,您可以使Dictionary漂亮;另一个建议是将其实现为扩展方法

 public static class CustomTypeExtensions {
   private static Dictionary<CustomType, Type> s_Map = new Dictionary<CustomType, Type>() {
     {CustomType.Datetime,  typeof(DateTime)},
     {CustomType.Date, typeof(DateTime}, 
     ...
   }; 

   public static Type ToType(this CustomType value) {
     if (s_Map.TryGetValue(value, out var result))
       return result;
     else
       return typeof(string); // when not found, return string 
   }
 }

....

var custom = CustomType.Bittype;

...

Type t = custom.ToType();   

【讨论】:

    【解决方案2】:

    Dmitry 的方法很好,你也可以使用这样的方法:

    class Example
    {
        private readonly HashSet<CustomType> _stringCompatibleTypes = new HashSet<CustomType>
        {
            CustomType.Char, CustomType.Fromtotype, CustomType.Nclob, ...
        };
    
        private readonly HashSet<CustomType> _dateCompatibleTypes = new HashSet<CustomType>
        {
            CustomType.Datetime, CustomType.Timestamp3, CustomType.Timestamp6, ...
        };
    
        // Another type sets
    
        public Type Foo(CustomType dataType)
        {
            if (_stringCompatibleTypes.Contains(dataType))
            {
                return typeof(string);
            }
    
            if (_dateCompatibleTypes.Contains(dataType))
            {
                return typeof(DateTime);
            }
    
            ...
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-17
      • 2023-03-15
      • 2021-02-10
      • 1970-01-01
      • 2011-03-07
      • 2019-02-07
      相关资源
      最近更新 更多