IBinarySerialize.Read(BinaryReader) 方法

定义

从用户定义类型 (UDT) 或用户定义聚合的二进制格式生成用户定义的类型或用户定义的聚合。

public:
 void Read(System::IO::BinaryReader ^ r);
public void Read (System.IO.BinaryReader r);
abstract member Read : System.IO.BinaryReader -> unit
Public Sub Read (r As BinaryReader)

参数

r
BinaryReader

从中对对象进行反序列化的 BinaryReader 流。

示例

以下示例演示 UDT 方法的 Read 实现,该 UDT 使用 BinaryReader 取消序列化以前保留的 UDT。 此示例假定 UDT 有两个数据属性: StringValueDoubleValue

// The binary layout is as follows:
//    Bytes 0 - 19: string text, padded to the right with null characters
//    Bytes 20+: Double value

// using Microsoft.SqlServer.Server;
public void Read(System.IO.BinaryReader r)
{

    int maxStringSize = 20;
    char[] chars;
    int stringEnd;
    string stringValue;
    double doubleValue;

    // Read the characters from the binary stream.
    chars = r.ReadChars(maxStringSize);

    // Find the start of the null character padding.
    stringEnd = Array.IndexOf(chars, '\0');

    if (stringEnd == 0)
    {
        stringValue = null;
        return;
    }

    // Build the string from the array of characters.
    stringValue = new String(chars, 0, stringEnd);

    // Read the double value from the binary stream.
    doubleValue = r.ReadDouble();

    // Set the object's properties equal to the values.
    this.StringValue = stringValue;
    this.DoubleValue = doubleValue;
}

注解

方法 Read 必须使用 方法写入 Write 的信息重新构造对象。

适用于