C# #区域IBinarySerialize上的字段顺序
我创建了C# #区域IBinarySerialize上的字段顺序,c#,sql-server,serialization,sqlclr,user-defined-aggregate,C#,Sql Server,Serialization,Sqlclr,User Defined Aggregate,我创建了IBinarySerialize区域,以便为用户定义的聚合创建CLR。我试图在C#中复制我的XIRR函数,只是为了学习如何使用CLR。我以相同的顺序写入和读取,但当我尝试构建时,出现错误: 严重性代码说明项目文件行抑制状态 错误CS1503参数1:无法从转换 “System.Data.SqlTypes.SqlDateTime”到 “bool”CustomAggregates D:\Transfer\CustomSqlAggregates\CustomAggregates\XIRR.cs
IBinarySerialize
区域,以便为用户定义的聚合创建CLR。我试图在C#中复制我的XIRR函数,只是为了学习如何使用CLR。我以相同的顺序写入和读取,但当我尝试构建时,出现错误:
严重性代码说明项目文件行抑制状态错误CS1503参数1:无法从转换 “System.Data.SqlTypes.SqlDateTime”到 “bool”CustomAggregates D:\Transfer\CustomSqlAggregates\CustomAggregates\XIRR.cs 255处于活动状态 错误来自写入部分 根据我在示例中看到的一些示例,我似乎无法找到我在这种安排中缺少的内容。 这是结构的get/set
[Serializable]
[SqlUserDefinedAggregate(
Format.Native,
IsInvariantToDuplicates = false, // Receiving the same value again changes the result
IsInvariantToNulls = false, // Receiving a NULL value changes the result
IsInvariantToOrder = true, // The order of the values affects the result
IsNullIfEmpty = true, // If no values are given the result is null
MaxByteSize = -1, // Maximum size of the aggregate instance. -1 represents a value larger than 8000 bytes, up to 2 gigabytes
Name = "XIRR" // Name of the aggregate
)]
public struct XIRR : IBinarySerialize
{
/// <summary>
/// Used to store the product
/// </summary>
public SqlDouble Result { get; private set; }
public SqlDouble Cashflow { get; private set; }
public SqlDateTime CashflowDateTime { get; private set; }
public bool HasValue { get; private set; }
public List<CashItem> CashFlows { get; private set;}
...
#region IBinarySerialize
/// <summary>
/// Writes the values to the stream in order to be stored
/// </summary>
/// <param name="write">The BinaryWriter stream</param>
public void Write(System.IO.BinaryWriter write)
{
write.Write(Result);
write.Write(Cashflow); //Line - 255
write.Write(CashflowDateTime);
write.Write(HasValue);
}
/// <summary>
/// Reads the values from the stream
/// </summary>
/// <param name="read">The BinaryReader stream</param>
public void Read(System.IO.BinaryReader read)
{
Result = new SqlDouble(read.ReadDouble());
Cashflow = new SqlDouble(read.ReadDouble());
CashflowDateTime = new SqlDateTime(Convert.ToDateTime(read.ReadString()));
HasValue = read.ReadBoolean();
}
#endregion IBinarySerialize
}
[可序列化]
[SqlUserDefinedAggregate(
格式。本机,
IsInvariantToDuplicates=false,//再次接收相同的值会更改结果
IsInvariantToNulls=false,//接收空值会更改结果
IsInvariantToOrder=true,//值的顺序会影响结果
IsNullIfEmpty=true,//如果没有给出任何值,则结果为null
MaxByteSize=-1,//聚合实例的最大大小。-1表示大于8000字节的值,最大可达2 GB
Name=“XIRR”//aggregate的名称
)]
公共结构XIRR:IBinarySerialize
{
///
///用于存储产品
///
公共SqlDouble结果{get;private set;}
公共SqlDouble现金流{get;private set;}
public SqlDateTime CashflowDateTime{get;private set;}
公共bool HasValue{get;private set;}
公共列表现金流{get;private set;}
...
#区域IBinarySerialize
///
///将值写入流以便存储
///
///二进制编写器流
公共无效写入(System.IO.BinaryWriter写入)
{
写。写(结果);
write.write(现金流);//第255行
write.write(现金流日期时间);
write.write(HasValue);
}
///
///从流中读取值
///
///二进制读取器流
公共无效读取(System.IO.BinaryReader读取)
{
结果=新的SqlDouble(read.ReadDouble());
现金流=新的SqlDouble(read.ReadDouble());
CashflowDateTime=newsqldatetime(Convert.ToDateTime(read.ReadString());
HasValue=read.ReadBoolean();
}
#末端区域IBinarySerialize
}
提前谢谢
如果您想让我提供更多信息,请告诉我。问题很可能是您的类型不匹配。
结果
、现金流
和现金流日期时间
的确切类型是什么?根据Read
方法,它们是Sql*
类型。它们真的是这样宣布的吗?或者它们被声明为Double
和DateTime
至少我认为您在两个方向上都没有正确处理CashflowDateTime
。假设CashflowDateTime
确实是SqlDateTime
,那么我猜您可能需要在Write
方法中替换这一行:
write.write(现金流日期时间);
使用以下两行代码:
write.write(CashflowDateTime.DayTicks);
write.write(CashflowDateTime.TimeTicks);
然后,在Read
方法中替换该行:
CashflowDateTime=newsqldatetime(Convert.ToDateTime(read.ReadString());
使用以下内容(从“DayTicks”和“TimeTicks”值重建SqlDateTime
):
CashflowDateTime=newsqldatetime(read.ReadInt32(),read.ReadInt32());
此外:
#region IBinarySerialize
在这里没有任何功能。删除它不会影响代码问题很可能是类型不匹配。
结果
、现金流
和现金流日期时间
的确切类型是什么?根据Read
方法,它们是Sql*
类型。它们真的是这样宣布的吗?或者它们被声明为Double
和DateTime
至少我认为您在两个方向上都没有正确处理CashflowDateTime
。假设CashflowDateTime
确实是SqlDateTime
,那么我猜您可能需要在Write
方法中替换这一行:
write.write(现金流日期时间);
使用以下两行代码:
write.write(CashflowDateTime.DayTicks);
write.write(CashflowDateTime.TimeTicks);
然后,在Read
方法中替换该行:
CashflowDateTime=newsqldatetime(Convert.ToDateTime(read.ReadString());
使用以下内容(从“DayTicks”和“TimeTicks”值重建SqlDateTime
):
CashflowDateTime=newsqldatetime(read.ReadInt32(),read.ReadInt32());
此外:
#region IBinarySerialize
在这里没有任何功能。删除它不会影响代码请显示CashFlowDateTime的声明及其数据类型n还请指出D:\Transfer\CustomSqlAggregates\CustomAggregates\XIRR.cs中的第255行。顺便说一下,样式指南几乎一致建议不要使用区域。它们是为一个特定目的而发明的-在.net 1.0 winforms中隐藏设计器代码。请显示CashFlowDateTime的声明及其数据类型n还请指出D:\Transfer\CustomSqlAggregates\CustomAggregates\XIRR.cs中的第255行。顺便说一句,样式指南几乎一致建议不要使用区域。它们是为一个特定的目的而发明的