Biblioteca de clases de .NET Framework
ObjectHandle (Clase)

Actualización: noviembre 2007

Ajusta referencias de objetos calculadas por valor. De este modo, se pueden devolver a través de un direccionamiento indirecto.

Espacio de nombres:  System.Runtime.Remoting
Ensamblado:  mscorlib (en mscorlib.dll)
Sintaxis

Visual Basic (Declaración)
<ClassInterfaceAttribute(ClassInterfaceType.AutoDual)> _
<ComVisibleAttribute(True)> _
Public Class ObjectHandle _
    Inherits MarshalByRefObject _
    Implements IObjectHandle
Visual Basic (Uso)
Dim instance As ObjectHandle
C#
[ClassInterfaceAttribute(ClassInterfaceType.AutoDual)]
[ComVisibleAttribute(true)]
public class ObjectHandle : MarshalByRefObject, 
    IObjectHandle
Visual C++
[ClassInterfaceAttribute(ClassInterfaceType::AutoDual)]
[ComVisibleAttribute(true)]
public ref class ObjectHandle : public MarshalByRefObject, 
    IObjectHandle
J#
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.AutoDual) */
/** @attribute ComVisibleAttribute(true) */
public class ObjectHandle extends MarshalByRefObject implements IObjectHandle
JScript
public class ObjectHandle extends MarshalByRefObject implements IObjectHandle
Comentarios

La clase ObjectHandle se utiliza para pasar un objeto (en un estado ajustado) entre varios dominios de aplicaciones sin cargar los metadatos del objeto ajustado en cada AppDomain a través del cual viaja ObjectHandle. De este modo, la clase ObjectHandle permite al llamador controlar el momento en que se carga Type del objeto remoto en un dominio.

Ejemplos

En el siguiente ejemplo de código se muestra cómo crear un objeto en otro AppDomain, así como recuperar el proxy del objeto de un ObjectHandle. En este ejemplo, se asume que el código de la clase MyType se compila en un ensamblado denominado "ObjectHandleAssembly".

Visual Basic
Imports System
Imports System.Runtime.Remoting


Public Class MyType
   Inherits MarshalByRefObject

   Public Sub New()
      Console.Write("Created an instance of MyType in an AppDomain with the ")
      Console.WriteLine("hashcode {0}", AppDomain.CurrentDomain.GetHashCode())
      Console.WriteLine("")
   End Sub 'New


   Public Function GetAppDomainHashCode() As Integer
      Return AppDomain.CurrentDomain.GetHashCode()
   End Function 'GetAppDomainHashCode

End Class 'MyType


Class Test

   Public Shared Sub Main()

      Console.WriteLine("The hash code of the default AppDomain is {0}.", AppDomain.CurrentDomain.GetHashCode())
      Console.WriteLine("")

      ' Creates another AppDomain.
      Dim domain As AppDomain = AppDomain.CreateDomain("AnotherDomain", Nothing, CType(Nothing, AppDomainSetup))

      ' 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())

   End Sub 'Main 

End Class 'Test 
C#
using System;
using System.Runtime.Remoting;


public class MyType : MarshalByRefObject {

   public MyType()  {
      Console.Write("Created an instance of MyType in an AppDomain with the ");
      Console.WriteLine("hash code {0}",AppDomain.CurrentDomain.GetHashCode());
      Console.WriteLine("");
   }

   public int GetAppDomainHashCode() {
      return AppDomain.CurrentDomain.GetHashCode();
   }
}


class Test {

   public static void Main() {

      Console.WriteLine("The hash code of the default AppDomain is {0}.", 
                        AppDomain.CurrentDomain.GetHashCode());
      Console.WriteLine("");

      // Creates another AppDomain.
      AppDomain domain = AppDomain.CreateDomain("AnotherDomain", 
                                                null, 
                                                (AppDomainSetup)null);

      // 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());
   }
}
Visual C++
using namespace System;
using namespace System::Runtime::Remoting;
public ref class MyType: public MarshalByRefObject
{
public:
   MyType()
   {
      Console::Write( "Created an instance of MyType in an AppDomain with the " );
      Console::WriteLine( "hash code {0}", AppDomain::CurrentDomain->GetHashCode() );
      Console::WriteLine( "" );
   }

   int GetAppDomainHashCode()
   {
      return AppDomain::CurrentDomain->GetHashCode();
   }

};

int main()
{
   Console::WriteLine( "The hash code of the default AppDomain is {0}.", AppDomain::CurrentDomain->GetHashCode() );
   Console::WriteLine( "" );

   // Creates another AppDomain.
   AppDomain^ domain = AppDomain::CreateDomain( "AnotherDomain", nullptr, (AppDomainSetup^)nullptr );

   // Creates an instance of MyType defined in the assembly called ObjectHandleAssembly.
   ObjectHandle^ obj = domain->CreateInstance( "ObjectHandleAssembly", "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( "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() );

}

Jerarquía de herencia

System..::.Object
  System..::.MarshalByRefObject
    System.Runtime.Remoting..::.ObjectHandle
Seguridad para subprocesos

Todos los miembros static (Shared en Visual Basic) públicos de este tipo son seguros para la ejecución de subprocesos. No se garantiza que los miembros de instancias sean seguros para la ejecución de subprocesos.
Plataformas

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

.NET Framework y .NET Compact Framework no admiten todas las versiones de cada plataforma. Para obtener una lista de las versiones compatibles, vea Requisitos de sistema de .NET Framework.
Información de versión

.NET Framework

Compatible con: 3.5, 3.0, 2.0, 1.1, 1.0
Vea también

Referencia

Etiquetas :


Page view tracker