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 資料流。

範例

下列範例示範 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 撰寫的資訊來重新建構物件。

適用於