IBinarySerialize.Read(BinaryReader) Method

Definition

Generates a user-defined type (UDT) or user-defined aggregate from its binary form.

C#
public void Read (System.IO.BinaryReader r);

Parameters

r
BinaryReader

The BinaryReader stream from which the object is deserialized.

Examples

The following example shows the implementation of the Read method of a UDT, which uses a BinaryReader to de-serialize a previously persisted UDT. This example assumes that the UDT has two data properties: StringValue and DoubleValue.

C#
// 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;
}

Remarks

The Read method must reconstitute your object using the information written by the Write method.

Applies to

Product Versions
SqlClient .NET Core 1.0, 1.1, 2.0, 2.1, 3.0, 3.1, 4.0, 4.1
SqlClient .NET Framework 1.0, 1.1, 2.0, 2.1, 3.0, 3.1, 4.0, 4.1
SqlClient .NET Standard 1.0, 1.1, 2.0, 2.1, 3.0, 3.1, 4.0, 4.1