我得到了一个让我感到困惑的奇怪错误:我有一个函数..uhm ...这样的事情:
void someFunctionTakingALong( long ID)
{
var table = new DataTable();
table .Columns.Add(new DataColumn("RID", Type.GetType("System.Int64")));
table.Rows.Add(ID); //<-- throws err
}
Run Code Online (Sandbox Code Playgroud)
抛出此错误:
System.ArgumentException: Input string was not in a correct format.Couldn't store in RID Column. Expected type is Int64. ---> System.FormatException: Input string was not in a correct format.
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseInt64(String value, NumberStyles options, NumberFormatInfo numfmt)
at System.String.System.IConvertible.ToInt64(IFormatProvider provider)
at System.Data.Common.Int64Storage.Set(Int32 record, Object value)
at System.Data.DataColumn.set_Item(Int32 record, Object value)
--- End of inner exception stack trace ---
Run Code Online (Sandbox Code Playgroud)
这种情况发生在生产中,而且我没有日志来查看实际传递给函数的ID的值...但即便如此......如果参数为a,则至少要保证ID是一个很长的... ?那为什么要扔?哪些值可以ID不能转换为int64?
编辑:
以下是实际来源:Trhowing列是PropValueID
public void AddRule(int PropID, long PropValueID, int type, string Data)
{
if (!m_HasRule)
{
m_Rules = new DataTable();
m_Rules.Columns.Add(new DataColumn("RID", Type.GetType("System.Int32")));
m_Rules.Columns.Add(new DataColumn("PropID", Type.GetType("System.Int32")));
m_Rules.Columns.Add(new DataColumn("PropValueID", Type.GetType("System.Int64")));
//m_Rules.Columns.Add(new DataColumn("PropValue", Type.GetType("System.String")));
m_Rules.Columns.Add(new DataColumn("Type", Type.GetType("System.Int32")));
m_Rules.Columns.Add(new DataColumn("Data", Type.GetType("System.String")));
m_HasRule = true;
}
ToStringSB = null;
if (type<2) // a "Is/Not specified property"
{
if (string.IsNullOrEmpty(Data)) //is specified (0,1)
m_Rules.Rows.Add(m_RID, PropID, 0, type, ""); //<<-- here we pass 0 (int32) instead of long.. could this be it? Stack says this is not the line throwing
else //is equal to/is not equal to(2,3)
m_Rules.Rows.Add(m_RID, PropID, PropValueID,3-type, Data);
}else
if ((type > 3) && (type < 6)) // a "like/not like" rule
{
// "Like" or "not like" DATA .. no value ID is provided
m_Rules.Rows.Add(m_RID, PropID, PropValueID, type, Data); //<<-- Stack says it throws here
}
else // a greater/lesser rule
{
m_Rules.Rows.Add(m_RID, PropID, PropValueID, type, Data);
}
}
Run Code Online (Sandbox Code Playgroud)
有一个可疑行,我们传递文字0(一个int),但这不是堆栈说它抛出的行号.
无法重现 - 这对我来说很好:
using System;
using System.Data;
class Test
{
static void Main()
{
long x = 10;
var table = new DataTable();
table.Columns.Add(new DataColumn("RID", Type.GetType("System.Int64")));
table.Rows.Add(x);
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,您可以Type.GetType()通过使用来摆脱调用typeof(long).
看起来由于某种原因它将值转换为字符串并返回,这真的很奇怪......你确定它只是来自那个代码吗?
如果你能想出一个简短而完整的例子,比如我的但却失败了,那真的会有所帮助.
| 归档时间: |
|
| 查看次数: |
4751 次 |
| 最近记录: |