共用方式為


RemotingServices.GetObjRefForProxy(MarshalByRefObject) 方法

定義

傳回 ObjRef,表示指定 Proxy 的遠端物件。

public:
 static System::Runtime::Remoting::ObjRef ^ GetObjRefForProxy(MarshalByRefObject ^ obj);
public static System.Runtime.Remoting.ObjRef GetObjRefForProxy (MarshalByRefObject obj);
[System.Security.SecurityCritical]
public static System.Runtime.Remoting.ObjRef GetObjRefForProxy (MarshalByRefObject obj);
static member GetObjRefForProxy : MarshalByRefObject -> System.Runtime.Remoting.ObjRef
[<System.Security.SecurityCritical>]
static member GetObjRefForProxy : MarshalByRefObject -> System.Runtime.Remoting.ObjRef
Public Shared Function GetObjRefForProxy (obj As MarshalByRefObject) As ObjRef

參數

obj
MarshalByRefObject

Proxy,其連接至您要建立 ObjRef 的物件。

傳回

ObjRef,表示指定之 Proxy 所連接的遠端物件,但如果尚未封送處理物件或 Proxy,則為 null

屬性

例外狀況

立即呼叫端沒有基礎結構使用權限。

範例

下列程式代碼範例示範如何取得 ObjRef 指定對象的實例。

ObjRef^ objRefSample = RemotingServices::GetObjRefForProxy( myRemoteObject );
Console::WriteLine( "***ObjRef Details***" );
Console::WriteLine( "URI:\t {0}", objRefSample->URI );
array<Object^>^channelData = objRefSample->ChannelInfo->ChannelData;
Console::WriteLine( "Channel Info:" );
IEnumerator^ myEnum = channelData->GetEnumerator();
while ( myEnum->MoveNext() )
{
   Object^ o = safe_cast<Object^>(myEnum->Current);
   Console::WriteLine( "\t {0}", o );
}

IEnvoyInfo^ envoyInfo = objRefSample->EnvoyInfo;
if ( envoyInfo == nullptr )
{
   Console::WriteLine( "This ObjRef does not have envoy information." );
}
else
{
   IMessageSink^ envoySinks = envoyInfo->EnvoySinks;
   Console::WriteLine( "Envoy Sink Class: {0}", envoySinks );
}

IRemotingTypeInfo^ typeInfo = objRefSample->TypeInfo;
Console::WriteLine( "Remote type name: {0}", typeInfo->TypeName );
Console::WriteLine( "Can my Object* cast to a Bitmap? {0}", typeInfo->CanCastTo( System::Drawing::Bitmap::typeid, objRefSample ) );
ObjRef objRefSample = RemotingServices.GetObjRefForProxy(myRemoteObject);

Console.WriteLine("***ObjRef Details***");
Console.WriteLine("URI:\t{0}", objRefSample.URI);

object[] channelData = objRefSample.ChannelInfo.ChannelData;

Console.WriteLine("Channel Info:");
foreach(object o in channelData)
    Console.WriteLine("\t{0}", o.ToString());

IEnvoyInfo envoyInfo = objRefSample.EnvoyInfo;

if (envoyInfo == null) {
    Console.WriteLine("This ObjRef does not have envoy information.");
}
else {
    IMessageSink envoySinks = envoyInfo.EnvoySinks;
    Console.WriteLine("Envoy Sink Class: {0}", envoySinks);
}

IRemotingTypeInfo typeInfo = objRefSample.TypeInfo;
Console.WriteLine("Remote type name: {0}", typeInfo.TypeName);

Console.WriteLine("Can my object cast to a Bitmap? {0}",
    typeInfo.CanCastTo(typeof(System.Drawing.Bitmap), objRefSample));
Dim objRefSample As ObjRef = RemotingServices.GetObjRefForProxy(myRemoteObject)

Console.WriteLine("***ObjRef Details***")
Console.WriteLine("URI:" + ControlChars.Tab + "{0}", objRefSample.URI)

Dim channelData As Object() = objRefSample.ChannelInfo.ChannelData
Console.WriteLine("Channel Info:")

Dim o As Object
For Each o In  channelData
   Console.WriteLine(ControlChars.Tab + "{0}", o.ToString())
Next o

Dim envoyInfo As IEnvoyInfo = objRefSample.EnvoyInfo
If envoyInfo Is Nothing Then
   Console.WriteLine("This ObjRef does not have envoy information.")
Else
   Dim envoySinks As IMessageSink = envoyInfo.EnvoySinks
   Console.WriteLine("Envoy Sink Class: {0}", envoySinks)
End If

Dim typeInfo As IRemotingTypeInfo = objRefSample.TypeInfo
Console.WriteLine("Remote type name: {0}", typeInfo.TypeName)

Console.WriteLine("Can my object cast to a Bitmap? {0}", typeInfo.CanCastTo(GetType(System.Drawing.Bitmap), objRefSample))

備註

ObjRef是物件可串行化的表示法,用來跨應用程式域界限傳輸對象參考。 ObjRef建立對象的 稱為封送處理。 ObjRef可以透過通道傳輸到另一個應用程式域, (可能在另一個進程或計算機上) 。 在其他應用程式域中, ObjRef 必須剖析 ,才能為物件建立 Proxy,通常連接到實際物件。 這項作業稱為「取消封擷取」。 在取消封存期間,會 ObjRef 剖析 以擷取遠端物件的方法資訊,並同時建立透明 Proxy 和 RealProxy 物件。

ObjRef包含描述Type要封送處理之物件的和類別、可唯一識別特定物件實例的 URI,以及如何連線到物件所在遠端應用程式的相關信息。

適用於