从数据库转换和返回通用值

时间:2014-02-19 14:18:56

标签: c#

我想从数据库返回值。 我定义了表,其中每列都定义了数据类型:

public enum TTableOffers
{
    [CStringValue("id")]
    [CType(typeof(Integer))]
    Id = 0,
    [CStringValue("project")]
    [CType(typeof(String))]
    ProjectId = 1,

}

所以列“id”是Integer类型,“project”是String等。 现在我希望通过这个枚举来获取特定列的值,如下所示:

    public T Value<T>(Enum enumColumn) where T : class // <<< class!
    {
        object aResult = m_aSqlDataReader.GetValue(columnIndex); // Here the aResult is filled with correct value (object) from database (working fine)

        ...     

        Type colType = CTypeAttribute.GetTypeValue(enumColumn); // Read the type from table - Integer or String (this is working fine)

        if (aResult == null) 
            return default(T);
        else
        {
           if (Convert.IsDBNull(aResult))
              return default(T);
           else
              aResult = ChangeType(aResult, colType); // Change the type to desired one --- here is the crash!!!                             
        }

        return (T)aResult;
       // here I also tried: return aResult as T;
    }

方法 ChangeType 我使用此方法:Invalid cast from 'System.Int32' to 'System.Nullable`1[[System.Int32, mscorlib]]

类型为String的列工作正常,但使用Integer时出现问题。

因为我不能使用Int32(这是struct)所以我定义了我自己的类Integer:

public class Integer : IComparable, IFormattable, IConvertible, IComparable<Integer>, IEquatable<Integer>
{
    int value = 0;

    public Integer(int value)
    {
        this.value = value;
    }

    // (Integer)123
    public static implicit operator Integer(int value)
    {
        return new Integer(value);
    }

    // (int)myInteger
    public static implicit operator int(Integer integer)
    {
        if (integer == null)
            integer = new Integer(default(int));

        return integer.value;
    }

    public static int operator +(Integer one, Integer two)
    {
        return one.value + two.value;
    }

    public static Integer operator +(int one, Integer two)
    {
        return new Integer(one + two);
    }

    public static int operator -(Integer one, Integer two)
    {
        return one.value - two.value;
    }

    public static Integer operator -(int one, Integer two)
    {
        return new Integer(one - two);
    }

    public static bool operator >(Integer one, int two)
    {
        return (int)one > two;
    }

    public static bool operator <(Integer one, int two)
    {
        return (int)one < two;
    }

    public static bool operator >(int one, Integer two)
    {
        return one > (int)two;
    }

    public static bool operator <(int one, Integer two)
    {
        return one < (int)two;
    }

    public TypeCode GetTypeCode()
    {
        throw new NotImplementedException();
    }

    public bool ToBoolean(IFormatProvider provider)
    {
        throw new NotImplementedException();
    }

    public byte ToByte(IFormatProvider provider)
    {
        throw new NotImplementedException();
    }

    public char ToChar(IFormatProvider provider)
    {
        throw new NotImplementedException();
    }

    public DateTime ToDateTime(IFormatProvider provider)
    {
        throw new NotImplementedException();
    }

    public decimal ToDecimal(IFormatProvider provider)
    {
        throw new NotImplementedException();
    }

    public double ToDouble(IFormatProvider provider)
    {
        throw new NotImplementedException();
    }

    public short ToInt16(IFormatProvider provider)
    {
        throw new NotImplementedException();
    }

    public int ToInt32(IFormatProvider provider)
    {
        throw new NotImplementedException();
    }

    public long ToInt64(IFormatProvider provider)
    {
        throw new NotImplementedException();
    }

    public sbyte ToSByte(IFormatProvider provider)
    {
        throw new NotImplementedException();
    }

    public float ToSingle(IFormatProvider provider)
    {
        throw new NotImplementedException();
    }

    public string ToString(IFormatProvider provider)
    {
        throw new NotImplementedException();
    }

    public object ToType(Type conversionType, IFormatProvider provider)
    {
        throw new NotImplementedException();
    }

    public ushort ToUInt16(IFormatProvider provider)
    {
        throw new NotImplementedException();
    }

    public uint ToUInt32(IFormatProvider provider)
    {
        throw new NotImplementedException();
    }

    public ulong ToUInt64(IFormatProvider provider)
    {
        throw new NotImplementedException();
    }

    public int CompareTo(object obj)
    {
        throw new NotImplementedException();
    }

    public string ToString(string format, IFormatProvider formatProvider)
    {
        throw new NotImplementedException();
    }

    public int CompareTo(Integer other)
    {
        throw new NotImplementedException();
    }

    public bool Equals(Integer other)
    {
        throw new NotImplementedException();
    }
}

对于String,一切正常,但是对于Integer,我得到了“从'System.Int32'到'Code.Data.Integer'的无效转换。”异常。

我在Integer类的每一行都设置了断点,没有人被击中。我假设我在类Integer中缺少一些重要的东西,因为它从未被击中,也没有完成转换。

我错过了什么?

2 个答案:

答案 0 :(得分:1)

您的问题是您的值(在本例中为int)未实现IConvertible接口。来自MSDN documentation

  

ChangeType是一种通用的转换方法,用于转换   value为conversionType指定的对象。值参数可以   是任何类型的对象,conversionType也可以是Type对象   表示任何基本类型或自定义类型。 转换为   成功,价值必须实施IConvertible接口,因为   方法只是调用适当的IConvertible方法。该   方法要求将值转换为conversionType   支撑。

此外,请参阅IConvertible上的文档:

  

如果您实施IConvertible接口,您的实现将   如果是Object,则由Convert.ChangeType方法自动调用   parameter是您的实现类型和Type的实例   参数是公共语言运行时类型。

您正在以相反的方式调用它:从CLR类型(int)转到您的自定义类型,这不会起作用。

答案 1 :(得分:0)

为什么不像这样制作两个Value方法:

public Nullable<T> Value<T>(Enum enumColumn) where T : struct {...}

public string Value(Enum enumColumn) {...}

第一个允许你返回空整数等。

更清洁,不是吗? :)