CÁC GIẢI PHÁP LẬP TRÌNH C# - Trang 639

639

Chương 16: Các giao diện và mẫu thông dụng

// các thành viên dữ liệu tùy biến.

public CustomException(string message, string stringInfo,

bool booleanInfo): this(message) {

this.stringInfo = stringInfo;

this.booleanInfo = booleanInfo;

}

public CustomException(string message, Exception inner,

string stringInfo, bool booleanInfo) : this(message, inner) {

this.stringInfo = stringInfo;

this.booleanInfo = booleanInfo;

}

// Các thuộc tính chỉ-đọc cho phép truy xuất đến các

// thành viên dữ liệu tùy biến.

public string StringInfo {

get { return stringInfo; }

}

public bool BooleanInfo {

get { return booleanInfo; }

}

// Phương thức GetObjectData (được khai báo trong giao diện

// ISerializable) được sử dụng trong quá trình tuần tự hóa

// CustomException. Vì CustomException có khai báo các thành

// viên dữ liệu tùy biến nên nó phải chép đè hiện thực

// GetObjectData của lớp cơ sở.

public override void GetObjectData(SerializationInfo info,

StreamingContext context) {

// Tuần tự hóa các thành viên dữ liệu tùy biến.

info.AddValue("StringInfo", stringInfo);

info.AddValue("BooleanInfo", booleanInfo);

// Gọi lớp cơ sở để tuần tự hóa các thành viên của nó.

base.GetObjectData(info, context);

}