1

我创建了IBinarySerialize区域以便为我的用户定义聚合创建我的 CLR。我试图在 C# 中复制我的 XIRR 函数,只是为了学习如何使用 CLR。我将写入和读取的顺序相同,但是当我尝试构建时出现错误:

严重性代码描述项目文件行抑制状态
错误 CS1503 参数 1:无法从 'System.Data.SqlTypes.SqlDateTime' 转换为 'bool' CustomAggregates D:\Transfer\CustomSqlAggregates\CustomAggregates\XIRR.cs 255 活动

错误来自写入部分。

在我在示例中看到的一些示例之后,我似乎无法找到我在这种安排中缺少的内容。这是结构的获取/设置

[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
}

提前致谢。

如果您希望我提供更多信息,请告诉我。

4

1 回答 1

0

问题很可能是您的类型不匹配。ResultCashflow和的具体类型是CashflowDateTime什么?根据Read方法,它们是Sql*类型。真的是这样声明的吗?或者它们被声明为Doubleand DateTime

至少我认为你CashflowDateTime在两个方向上都处理不正确。假设这CashflowDateTime确实是 a SqlDateTime,那么我猜您可能需要在Write方法中替换这一行:

write.Write(CashflowDateTime);

使用以下两行:

write.Write(CashflowDateTime.DayTicks);
write.Write(CashflowDateTime.TimeTicks);

然后,在方法中替换这一行Read

CashflowDateTime = new SqlDateTime(Convert.ToDateTime(read.ReadString()));

使用以下内容(SqlDateTime从“DayTicks”和“TimeTicks”值重建):

CashflowDateTime = new SqlDateTime(read.ReadInt32(), read.ReadInt32());

还:

  1. 在这里#region IBinarySerialize没有任何功能。删除它不会影响代码。
  2. 有关使用 SQLCLR 的更多信息,请参阅:SQLCLR 信息
于 2019-09-30T23:42:13.290 回答