C#:Clever Helper Class返回系统数据类型

时间:2011-07-13 19:45:27

标签: c# .net

我正在使用一个只能支持我编写代码的数据类型的数据库。

我创建了一个Enum参数,其中包含可用类型列表。

public enum TableDataType
{
  None=0, String=1, Integer=2, Character=3, Boolean=4, DateTime=5, Decimal=6
}

这样可行,但我仍然需要处理进入和退出我的数据结构:

TableDataType GetMyType(DataGridViewColumn col) {
  TableDataType type;
  if (col.ValueType == typeof(bool)) {
    type = TableDataType.Boolean;
  } else if (col.ValueType == typeof(char)) {
    type = TableDataType.Character;
  } else if (col.ValueType == typeof(DateTime)) {
    type = TableDataType.DateTime;
  } else if (col.ValueType == typeof(Decimal)) {
    type = TableDataType.Decimal;
  } else if (col.ValueType == typeof(Int32)) {
    type = TableDataType.Integer;
  } else if (col.ValueType == typeof(string)) {
    type = TableDataType.String;
  } else {
    throw new ArgumentException(string.Format("Data Type of '{0}' is not supported.", col.ValueType));
  }
  return type;
}

然后有代码走另一条路......

Type GetSystemType(TableDataType myType) {
  Type sysType;
  switch (myType) {
    case TableDataType.Boolean: sysType = typeof(bool); break;
    case TableDataType.Character: sysType = typeof(char); break;
    case TableDataType.DateTime: sysType = typeof(DateTime); break;
    case TableDataType.Integer: sysType = typeof(Int32); break;
    case TableDataType.Decimal: sysType = typeof(Decimal); break;
    case TableDataType.String: sysType = typeof(string); break;
    default: throw new ArgumentOutOfRangeException(string.Format("Data Type '{0}' is not allowed.", cd.DataType));
  }
  return sysType;
}

问题来自于在我的代码中使用多个类似的例程。如果我告诉DataGridViewColumn它的格式为Int32,然后将其传递给Integer,则会出现ArgumentException错误。

我正在寻找一个好的,快速的类包装器,它巧妙地存储了值和一系列可接受的数据类型。

[编辑]我提出的解决方案:

使用harpo的评论和Bas解决方案中提供的信息,我创建了这个课程:

public static class Enumerate {

  private Enumerate() {
    throw new NotSupportedException();
  }

  public static SqlDbType ForSqlCe(System.Type item) {
    return sqlDbCode(item);
  }

  public static SqlDbType ForSqlCe(TableDataType item) {
    return sqlDbCode(item.GetType());
  }

  static SqlDbType sqlDbCode(System.Type item) {
    switch (Type.GetTypeCode(item)) {
      case TypeCode.Boolean: return SqlDbType.Bit;
      case TypeCode.Byte:
      case TypeCode.Char:
      case TypeCode.SByte: return SqlDbType.NChar;
      case TypeCode.DateTime: return SqlDbType.DateTime;
      case TypeCode.Decimal:
      case TypeCode.Double:
      case TypeCode.Single: return SqlDbType.Decimal;
      case TypeCode.Int16:
      case TypeCode.Int32:
      case TypeCode.Int64:
      case TypeCode.UInt16:
      case TypeCode.UInt32:
      case TypeCode.UInt64: return SqlDbType.Int;
      case TypeCode.String: return SqlDbType.NVarChar;
      case TypeCode.DBNull:
      case TypeCode.Empty:
      case TypeCode.Object:
      default: throw new TypeAccessException(item + " unknown");
    }
  }

  public static TableDataType ForTableData(SqlDbType item) {
    return tableDataCode(item.GetType());
  }

  public static TableDataType ForTableData(System.Type item) {
    return tableDataCode(item);
  }

  static TableDataType tableDataCode(System.Type item) {
    switch (Type.GetTypeCode(item)) {
      case TypeCode.Boolean: return TableDataType.Boolean;
      case TypeCode.Byte:
      case TypeCode.Char:
      case TypeCode.SByte: return TableDataType.Character;
      case TypeCode.DateTime: return TableDataType.DateTime;
      case TypeCode.Decimal:
      case TypeCode.Double:
      case TypeCode.Single: return TableDataType.Decimal;
      case TypeCode.Int16:
      case TypeCode.Int32:
      case TypeCode.Int64:
      case TypeCode.UInt16:
      case TypeCode.UInt32:
      case TypeCode.UInt64: return TableDataType.Integer;
      case TypeCode.String: return TableDataType.String;
      case TypeCode.DBNull:
      case TypeCode.Empty:
      case TypeCode.Object:
      default: throw new TypeAccessException(item + " unknown");
    }
  }

  public static Type ForWin32(string item) {
    string text = item.Trim().ToLower();
    switch (text) {
      case "boolean":
      case "bool":
      case "bit": return typeof(bool);
      case "byte":
      case "char":
      case "sbyte": return typeof(char);
      case "date":
      case "datetime":
      case "time": return typeof(DateTime);
      case "decimal":
      case "double":
      case "numeric":
      case "single": return typeof(Double);
      case "int":
      case "int16":
      case "int32":
      case "int64":
      case "integer":
      case "uint16":
      case "uint32":
      case "uint64": return typeof(Int32);
      case "string": return typeof(string);
      default:
        throw new TypeAccessException(item + " unknown");
    }
  }

  public static Type ForWin32(SqlDbType item) {
    return win32Code(item.GetType());
  }

  public static Type ForWin32(TableDataType item) {
    return win32Code(item.GetType());
  }

  static Type win32Code(System.Type item) {
    switch (Type.GetTypeCode(item)) {
      case TypeCode.Boolean: return typeof(bool);
      case TypeCode.Byte:
      case TypeCode.Char:
      case TypeCode.SByte: return typeof(char);
      case TypeCode.DateTime: return typeof(DateTime);
      case TypeCode.Decimal:
      case TypeCode.Double:
      case TypeCode.Single: return typeof(Decimal);
      case TypeCode.Int16:
      case TypeCode.Int32:
      case TypeCode.Int64:
      case TypeCode.UInt16:
      case TypeCode.UInt32:
      case TypeCode.UInt64: return typeof(Int32);
      case TypeCode.String: return typeof(string);
      case TypeCode.DBNull:
      case TypeCode.Empty:
      case TypeCode.Object:
      default: throw new TypeAccessException(item + " unknown");
    }
  }

}

1 个答案:

答案 0 :(得分:4)

如果您不想使用System.TypeCode建议的harpo,请使用Type.GetType()

string assemblyQualifiedName = "System.{0}, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";

string typeString = myEnumValue.ToString();
Type type = Type.GetType(string.Format(assemblyQualifiedName, typeString));

另一种选择是将映射存储在字典中:

static class TypeResolver
{
    static Dictionary<TableDataType, Type> typeLookup = new Dictionary<TableDataType, Type>();

    static TypeResolver()
    {
        typeLookup.Add(TableDataType.Integer, typeof(Int32));
        typeLookup.Add(TableDataType.String, typeof(String));
    }

    public static Type Resolve(TableDataType tableType)
    {
        return typeLookup[tableType];
    }
}