我的dbtype是否可以简化泛型?

时间:2012-11-05 20:37:35

标签: c# generics types

我有一个结构,我想更容易填写

    public struct DBParameter
    {
        public string parameterName;
        public object value;
        public DbType dbType;

        public DBParameter(string paramName, object val, DbType type)
        {
            parameterName = paramName;
            value = val;
            dbType = type;
        }
        public SqlParameter ToSqlParameter()
        {
            if (parameterName == string.Empty || parameterName == null)
                throw new ArgumentException("No parameter, fieldname is mandatory", "parameterName");

            SqlParameter me = new SqlParameter(parameterName, value);
            me.DbType = dbType;

            return me;
        }

我在考虑这样的事情......

        public void LoadDBParameter<T>(string paramName, T val)
        {
            parameterName = paramName;
            value = val;
            if (val is Int32)
            {
                dbType = DbType.Int32;
            }
            if (val is String)
            {
                dbType = DbType.String;
            }
            if (val is Int64)
            {
                dbType = DbType.Int64;
            }
            if (true)
            {
                //(...)
            }
        }
    }

但是我一直看到名字几乎相同,有没有比编写每个公共数据结构更简单的方法?

1 个答案:

答案 0 :(得分:4)

要从“精致”中窃取一些代码,我们就这样做了:

    static readonly Dictionary<Type, DbType> typeMap;

    static SqlMapper()
    {
        typeMap = new Dictionary<Type, DbType>();
        typeMap[typeof(byte)] = DbType.Byte;
        typeMap[typeof(sbyte)] = DbType.SByte;
        typeMap[typeof(short)] = DbType.Int16;
        typeMap[typeof(ushort)] = DbType.UInt16;
        typeMap[typeof(int)] = DbType.Int32;
        typeMap[typeof(uint)] = DbType.UInt32;
        typeMap[typeof(long)] = DbType.Int64;
        typeMap[typeof(ulong)] = DbType.UInt64;
        typeMap[typeof(float)] = DbType.Single;
        typeMap[typeof(double)] = DbType.Double;
        typeMap[typeof(decimal)] = DbType.Decimal;
        typeMap[typeof(bool)] = DbType.Boolean;
        typeMap[typeof(string)] = DbType.String;
        typeMap[typeof(char)] = DbType.StringFixedLength;
        typeMap[typeof(Guid)] = DbType.Guid;
        typeMap[typeof(DateTime)] = DbType.DateTime;
        typeMap[typeof(DateTimeOffset)] = DbType.DateTimeOffset;
        typeMap[typeof(TimeSpan)] = DbType.Time;
        typeMap[typeof(byte[])] = DbType.Binary;
        typeMap[typeof(byte?)] = DbType.Byte;
        typeMap[typeof(sbyte?)] = DbType.SByte;
        typeMap[typeof(short?)] = DbType.Int16;
        typeMap[typeof(ushort?)] = DbType.UInt16;
        typeMap[typeof(int?)] = DbType.Int32;
        typeMap[typeof(uint?)] = DbType.UInt32;
        typeMap[typeof(long?)] = DbType.Int64;
        typeMap[typeof(ulong?)] = DbType.UInt64;
        typeMap[typeof(float?)] = DbType.Single;
        typeMap[typeof(double?)] = DbType.Double;
        typeMap[typeof(decimal?)] = DbType.Decimal;
        typeMap[typeof(bool?)] = DbType.Boolean;
        typeMap[typeof(char?)] = DbType.StringFixedLength;
        typeMap[typeof(Guid?)] = DbType.Guid;
        typeMap[typeof(DateTime?)] = DbType.DateTime;
        typeMap[typeof(DateTimeOffset?)] = DbType.DateTimeOffset;
        typeMap[typeof(TimeSpan?)] = DbType.Time;
        typeMap[typeof(Object)] = DbType.Object;
    }

    internal static DbType LookupDbType(Type type, string name)
    {
        DbType dbType;
        var nullUnderlyingType = Nullable.GetUnderlyingType(type);
        if (nullUnderlyingType != null) type = nullUnderlyingType;
        if (type.IsEnum)
        {
            type = Enum.GetUnderlyingType(type);
        }
        if (typeMap.TryGetValue(type, out dbType))
        {
            return dbType;
        }
        ... what to do if no match
    }