方法 : リモート オブジェクトからスローできる例外の種類を作成する
RemotingException クラスから派生させ、ISerializable インターフェイスを実装することによって、リモート オブジェクトからスローし、リモートの呼び出し元でキャッチできる独自の例外の種類を作成できます。
リモート オブジェクトからスローし、リモートの呼び出し元でキャッチできる例外の種類を作成するには
1. RemotingException クラスから派生させるクラスを定義します。
Public Class RemotableType Inherits MarshalByRefObject End Class 'RemotableType
public class RemotableType : MarshalByRefObject{ }
SerializableAttribute 属性をクラスに含めます。
<Serializable()> Public Class CustomRemotableException Inherits RemotingException … End Class
[Serializable] public class CustomRemotableException : RemotingException, ISerializable { … }
SerializationInfo オブジェクトと StreamingContext オブジェクトをパラメータとして受け取る逆シリアル化コンストラクタを実装します。
使用例
リモート サーバー オブジェクトによってスローされたとき、構成されていれば呼び出し元にコピーバックされる簡単な実装のコード例を次に示します。
<Serializable()> Public Class CustomRemotableException
Inherits RemotingException
Private _internalMessage As String
Public Sub CustomRemotableException()
_internalMessage = String.Empty
End Sub
Public Sub CustomRemotableException(ByVal message As String)
_internalMessage = message
End Sub
Public Sub CustomRemotableException(ByVal info As _
SerializationInfo, ByVal context As StreamingContext)
_internalMessage = info.GetValue("_internalMessage", _
_internalMessage.GetType())
End Sub
Public Overrides Sub GetObjectData(ByVal info As SerializationInfo, _
ByVal context As StreamingContext)
info.AddValue("_internalMessage", _internalMessage)
End Sub
Public Shadows ReadOnly Property Message() As String
Get
Return "This is your custom remotable exception returning: \"" " +
_internalMessage + "\"";"
End Get
End Property
End Class
[Serializable]
public class CustomRemotableException : RemotingException, ISerializable {
private string _internalMessage;
public CustomRemotableException(){
_internalMessage = String.Empty;
}
public CustomRemotableException(string message){
_internalMessage = message;
}
public CustomRemotableException(SerializationInfo info, StreamingContext context){
_internalMessage = (string)info.GetValue("_internalMessage", typeof(string));
}
public override void GetObjectData(SerializationInfo info, StreamingContext context){
info.AddValue("_internalMessage", _internalMessage);
}
// Returns the exception information.
public override string Message{
get {
return "This is your custom remotable exception returning: \""
+ _internalMessage
+ "\"";
}
}
}
参照
関連項目
概念
リモート処理可能オブジェクトとリモート処理不可能オブジェクト