HOW TO:建立可由遠端物件擲回的例外狀況類型
您可以藉由從 RemotingException 類別衍生並實作 ISerializable 介面,建立可由遠端物件擲回並由遠端呼叫端攔截的例外狀況類型。
若要建立可由遠端物件擲回並由遠端呼叫端攔截的例外狀況類型
1.定義一個衍生自 RemotingException 類別的類別。
Public Class RemotableType Inherits MarshalByRefObject
Implements ISerializable
' ...
End Class 'RemotableType
Public Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)
_internalMessage = info.GetValue("_internalMessage", GetType(String))
End Sub
public CustomRemotableException(SerializationInfo info, StreamingContext context)
{
_internalMessage = (string)info.GetValue("_internalMessage", typeof(string));
}
範例
下列程式碼範例提供一個簡單的實作,一旦經過設定,此實作就會在遠端伺服器物件將它擲回時複製回呼叫端。
<Serializable()> Public Class CustomRemotableException
Inherits RemotingException
Implements ISerializable
Private _internalMessage As String
Public Sub New()
_internalMessage = String.Empty
End Sub
Public Sub New(ByVal message As String)
_internalMessage = message
End Sub
Public Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)
_internalMessage = info.GetValue("_internalMessage", GetType(String))
End Sub
Public Overrides Sub GetObjectData(ByVal info As SerializationInfo, ByVal context As StreamingContext)
info.AddValue("_internalMessage", _internalMessage)
End Sub
Public Overrides 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
+ "\"";
}
}
}
請參閱
參考
概念
Copyright © 2007 by Microsoft Corporation. All rights reserved.