ObjectHandle 類別

定義

包裝傳值封送處理的物件參考,並允許透過間接取值 (Indirection) 將它們傳回。

public ref class ObjectHandle : MarshalByRefObject
public ref class ObjectHandle : MarshalByRefObject, System::Runtime::Remoting::IObjectHandle
public class ObjectHandle : MarshalByRefObject
[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
type ObjectHandle = class
    inherit MarshalByRefObject
[<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
Public Class ObjectHandle
Inherits MarshalByRefObject
Implements IObjectHandle
繼承
ObjectHandle
屬性
實作

範例

下列程式碼範例示範如何在另一個 AppDomain 中建立 物件,並從 中擷取物件的 ObjectHandle Proxy。 在此範例中,您可以假設類別的程式 MyType 代碼已編譯成名為 「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

備註

類別 ObjectHandle 是用來在多個應用程式域之間傳遞包裝狀態 (的物件) ,而不需在移動的每個 AppDomainObjectHandle 應用程式中載入包裝物件的中繼資料。 因此,當遠端物件的 載入網域時 Type ,類別 ObjectHandle 會提供 呼叫端控制項。

建構函式

ObjectHandle(Object)

初始化包裝指定物件 oObjectHandle 類別的新執行個體。

方法

CreateObjRef(Type)

建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。

(繼承來源 MarshalByRefObject)
Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetLifetimeService()
已淘汰.

擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。

(繼承來源 MarshalByRefObject)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
InitializeLifetimeService()

初始化被包裝物件的存留期使用期 (Lifetime Lease)。

InitializeLifetimeService()
已淘汰.

取得存留期服務物件,以控制這個執行個體的存留期原則。

(繼承來源 MarshalByRefObject)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
MemberwiseClone(Boolean)

建立目前 MarshalByRefObject 物件的淺層複本。

(繼承來源 MarshalByRefObject)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)
Unwrap()

傳回被包裝的物件。

適用於