Unwrap Method
Collapse the table of content
Expand the table of content

ObjectHandle.Unwrap Method

Returns the wrapped object.

[Visual Basic]
Public Overridable Function Unwrap() As Object Implements _
   IObjectHandle.Unwrap
[C#]
public virtual object Unwrap();
[C++]
public: virtual Object* Unwrap();
[JScript]
public function Unwrap() : Object;

Return Value

The wrapped object.

Implements

IObjectHandle.Unwrap

Remarks

Notes to Callers:  An ObjectHandle is a remoted MarshalByRefObject that is tracked by the remoting lifetime service. A call to the current method can fail if the lifetime lease on the ObjectHandle expires.

Example

[Visual Basic, C#, C++] The following code example demonstrates how to activate an object in another AppDomain, retrieve a proxy to it with the Unwrap method, and use the proxy to access the remote object.

[Visual Basic] 
' Creates an instance of MyType defined in the assembly called ObjectHandleAssembly.
Dim obj As ObjectHandle = domain.CreateInstance("ObjectHandleAssembly", "MyType")

' Unwrapps the proxy to the MyType object created in the other AppDomain.
Dim testObj As MyType = CType(obj.Unwrap(), MyType)

If RemotingServices.IsTransparentProxy(testObj) Then
   Console.WriteLine("The unwrapped object is a proxy.")
Else
   Console.WriteLine("The unwrapped object is not a proxy!")
End If 
Console.WriteLine("")
Console.Write("Calling a method on the object located in an AppDomain with the hash code ")
Console.WriteLine(testObj.GetAppDomainHashCode())

[C#] 
// Creates an instance of MyType defined in the assembly called ObjectHandleAssembly.
ObjectHandle obj = domain.CreateInstance("ObjectHandleAssembly", "MyType");

// Unwrapps the proxy to the MyType object created in the other AppDomain.
MyType testObj = (MyType)obj.Unwrap();

if(RemotingServices.IsTransparentProxy(testObj))
   Console.WriteLine("The unwrapped object is a proxy.");
else
   Console.WriteLine("The unwrapped object is not a proxy!");    

Console.WriteLine("");
Console.Write("Calling a method on the object located in an AppDomain with the hash code ");
Console.WriteLine(testObj.GetAppDomainHashCode());

[C++] 
// Creates an instance of MyType defined in the assembly called ObjectHandleAssembly.
ObjectHandle*  obj = domain->CreateInstance(S"ObjectHandleAssembly", S"MyType");

// Unwraps the proxy to the MyType object created in the other AppDomain.
MyType* testObj = dynamic_cast<MyType*>(obj->Unwrap());

if (RemotingServices::IsTransparentProxy(testObj))
   Console::WriteLine(S"The unwrapped object is a proxy.");
else
   Console::WriteLine(S"The unwrapped object is not a proxy!");

Console::WriteLine(S"");
Console::Write(S"Calling a method on the object located in an AppDomain with the hash code ");
Console::WriteLine(testObj->GetAppDomainHashCode());

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

ObjectHandle Class | ObjectHandle Members | System.Runtime.Remoting Namespace

Show:
© 2016 Microsoft