在一个声明中铸造我有一个小工具方法,看起来像这样:拆箱和泛型方法
/// <summary>
/// Replaces a DBNull value with the default of the type specified
/// </summary>
/// <typeparam name="T">Resulting type</typeparam>
/// <param name="p_this">Object to check for DBNull</param>
/// <returns>p_this if it is not DBNull.Value, else default(T)</returns>
public static T ReplaceDBNullWithDefault<T>(this object p_this)
{
return p_this == System.DBNull.Value ? default(T) : (T)p_this;
}
在我的特定情况下,我拿着一个记录了一个数据表,并采取特定领域使用弱类型,我得到的具体领域是一个long
这是被装箱到object
。再现这样的一个例子是如下:
var obj = 2934L;
int num = obj.ReplaceDBNullWithDefault<int>();
它失败并InvalidCastException
,在(T)p_this
。
我明白为什么,盒装long
不能直接转换为int
,并试图像这样做也失败:
object myLong = 234L;
int myInt = (int)myLong;
然而,拆箱,然后浇注工作正常:
object myLong = 234L;
int myInt = (int)(long)myLong;
如何在我的方法中解决此问题?
难道你的理由阐述为什么你铸造'int',而不是'long'? (因此将'T'指定为'long') –
通过提供正确的类型;) –
我将结果放入的字段是“int”,而不是“long”。我想我可以在那里演出。 – Logan