RemotingServices.GetObjRefForProxy(MarshalByRefObject) 方法

定义

从指定的代理返回表示远程对象的 ObjRef

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

一个代理,连接到要为其创建 ObjRef 的对象。

返回

ObjRef,它表示指定的代理要连接到的远程对象;或者如果对象或代理未被封送,则为 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 必须分析 才能为对象创建代理,通常连接到实际对象。 此操作称为取消封送。 在取消封送期间, ObjRef 将分析 以提取远程对象的方法信息,并创建透明代理和 RealProxy 对象。

包含 ObjRef 描述 Type 被封送对象的 和 类的信息、唯一标识特定对象实例的 URI,以及有关如何访问对象所在远程应用程序的通信相关信息。

适用于