ObjectHandle Klasse
Definition
Wichtig
Einige Informationen beziehen sich auf Vorabversionen, die vor dem Release ggf. grundlegend überarbeitet werden. Microsoft übernimmt hinsichtlich der hier bereitgestellten Informationen keine Gewährleistungen, seien sie ausdrücklich oder konkludent.
Umschließt Marshal-by-Value-Objektverweise, sodass sie über eine Dereferenzierung zurückgegeben werden können.
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
- Vererbung
- Attribute
- Implementiert
Beispiele
Das folgende Codebeispiel zeigt, wie Sie ein Objekt in einem anderen AppDomainerstellen und einen Proxy für das Objekt aus einem ObjectHandleObjekt abrufen. In diesem Beispiel können Sie davon ausgehen, dass der Code der MyType Klasse in eine Assembly mit dem Namen "ObjectHandleAssembly" kompiliert wird.
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
Hinweise
Die ObjectHandle Klasse wird verwendet, um ein Objekt (in einem umbrochenen Zustand) zwischen mehreren Anwendungsdomänen zu übergeben, ohne die Metadaten für das umbrochene Objekt in jedem AppDomain Durchgangsobjekt zu laden.ObjectHandle Daher gibt die ObjectHandle Klasse dem Aufrufer die Steuerung, wann das Type Remoteobjekt in eine Domäne geladen wird.
Konstruktoren
| Name | Beschreibung |
|---|---|
| ObjectHandle(Object) |
Initialisiert eine Instanz der ObjectHandle Klasse, wobei das angegebene Objekt |
Methoden
| Name | Beschreibung |
|---|---|
| CreateObjRef(Type) |
Erstellt ein Objekt, das alle relevanten Informationen enthält, die zum Generieren eines Proxys erforderlich sind, der für die Kommunikation mit einem Remoteobjekt verwendet wird. (Geerbt von MarshalByRefObject) |
| Equals(Object) |
Bestimmt, ob das angegebene Objekt gleich dem aktuellen Objekt ist. (Geerbt von Object) |
| GetHashCode() |
Dient als Standardhashfunktion. (Geerbt von Object) |
| GetLifetimeService() |
Ruft das aktuelle Lebensdauerdienstobjekt ab, das die Lebensdauerrichtlinie für diese Instanz steuert. (Geerbt von MarshalByRefObject) |
| GetType() |
Ruft die Type der aktuellen Instanz ab. (Geerbt von Object) |
| InitializeLifetimeService() |
Initialisiert die Lebensdauer-Lease des umschlossenen Objekts. |
| MemberwiseClone() |
Erstellt eine flache Kopie der aktuellen Object. (Geerbt von Object) |
| MemberwiseClone(Boolean) |
Erstellt eine flache Kopie des aktuellen MarshalByRefObject Objekts. (Geerbt von MarshalByRefObject) |
| ToString() |
Gibt eine Zeichenfolge zurück, die das aktuelle Objekt darstellt. (Geerbt von Object) |
| Unwrap() |
Gibt das umschlossene Objekt zurück. |