IBinarySerialize.Read Method (BinaryReader)


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

Namespace:   Microsoft.SqlServer.Server
Assembly:  System.Data (in System.Data.dll)

void Read(
	BinaryReader r


Type: System.IO.BinaryReader

The BinaryReader stream from which the object is deserialized.

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

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.

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

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;

    // 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;

.NET Framework
Available since 2.0
Return to top