ObjectHandle Classe
Definição
Importante
Algumas informações se referem a produtos de pré-lançamento que podem ser substancialmente modificados antes do lançamento. A Microsoft não oferece garantias, expressas ou implícitas, das informações aqui fornecidas.
Encapsula referências de objeto marshal-by-value, permitindo que elas sejam retornadas por meio de uma indireção.
public ref class ObjectHandle : MarshalByRefObject, System::Runtime::Remoting::IObjectHandle
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDual)]
public class ObjectHandle : MarshalByRefObject, System.Runtime.Remoting.IObjectHandle
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDual)]
[System.Runtime.InteropServices.ComVisible(true)]
public class ObjectHandle : MarshalByRefObject, System.Runtime.Remoting.IObjectHandle
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDual)>]
type ObjectHandle = class
inherit MarshalByRefObject
interface IObjectHandle
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDual)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type ObjectHandle = class
inherit MarshalByRefObject
interface IObjectHandle
Public Class ObjectHandle
Inherits MarshalByRefObject
Implements IObjectHandle
- Herança
- Atributos
- Implementações
Exemplos
O exemplo de código a seguir mostra como criar um objeto em outro AppDomaine recuperar um proxy para o objeto de um ObjectHandle. Neste exemplo, você pode assumir que o código da MyType classe é compilado em um assembly chamado "ObjectHandleAssembly".
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() );
}
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());
}
}
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
Public Function GetAppDomainHashCode() As Integer
Return AppDomain.CurrentDomain.GetHashCode()
End Function 'GetAppDomainHashCode
End Class
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
End Class
Comentários
A ObjectHandle classe é usada para passar um objeto (em um estado encapsulado) entre vários domínios de aplicativo sem carregar os metadados para o objeto encapsulado em cada AppDomain um pelos quais o ObjectHandle percurso. Assim, a ObjectHandle classe fornece ao chamador o controle de quando o Type objeto remoto é carregado em um domínio.
Construtores
| Nome | Description |
|---|---|
| ObjectHandle(Object) |
Inicializa uma instância da ObjectHandle classe, encapsulando o objeto |
Métodos
| Nome | Description |
|---|---|
| CreateObjRef(Type) |
Cria um objeto que contém todas as informações relevantes necessárias para gerar um proxy usado para se comunicar com um objeto remoto. (Herdado de MarshalByRefObject) |
| Equals(Object) |
Determina se o objeto especificado é igual ao objeto atual. (Herdado de Object) |
| GetHashCode() |
Serve como a função hash predefinida. (Herdado de Object) |
| GetLifetimeService() |
Recupera o objeto de serviço de tempo de vida atual que controla a política de tempo de vida para essa instância. (Herdado de MarshalByRefObject) |
| GetType() |
Obtém o Type da instância atual. (Herdado de Object) |
| InitializeLifetimeService() |
Inicializa a concessão de tempo de vida do objeto encapsulado. |
| MemberwiseClone() |
Cria uma cópia superficial do Objectatual. (Herdado de Object) |
| MemberwiseClone(Boolean) |
Cria uma cópia superficial do objeto atual MarshalByRefObject . (Herdado de MarshalByRefObject) |
| ToString() |
Retorna uma cadeia de caracteres que representa o objeto atual. (Herdado de Object) |
| Unwrap() |
Retorna o objeto encapsulado. |