C#: Clever Helper Class to return System Data Type
I'm working with a database that is only capable of supporting data types that I have written code for.
I created an Enum
parameter with a list of available types.
public enum TableDataType
{
None=0, String=1, Integer=2, Character=3, Boolean=4, DateTime=5, Decimal=6
}
This works, but I still have to process going in and coming back out of my data structures:
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;
}
Then there is code going the other way...
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 A开发者_运维知识库rgumentOutOfRangeException(string.Format("Data Type '{0}' is not allowed.", cd.DataType));
}
return sysType;
}
The problem comes from using routines similar to these at more than one spot in my code. If I tell a DataGridViewColumn
that it is formatted for an Int32
then pass it an Integer
, I get ArgumentException
errors.
I'm looking for a good, fast class wrapper that cleverly stores the value and a select range of acceptable data types.
[EDIT] Solution I came up with:
Using the information provided in harpo's comment and Bas's solution, I created this class:
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");
}
}
}
If you dont want to use System.TypeCode like harpo suggests, use 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));
Another option is to store the mapping in a Dictionary:
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];
}
}
精彩评论