다음을 통해 공유


ObjectHandle 클래스

값으로 마샬링된 개체 참조를 래핑하여 간접 참조를 통해 반환할 수 있도록 합니다.

네임스페이스: System.Runtime.Remoting
어셈블리: mscorlib(mscorlib.dll)

구문

‘선언
<ComVisibleAttribute(True)> _
<ClassInterfaceAttribute(ClassInterfaceType.AutoDual)> _
Public Class ObjectHandle
    Inherits MarshalByRefObject
    Implements IObjectHandle
‘사용 방법
Dim instance As ObjectHandle
[ComVisibleAttribute(true)] 
[ClassInterfaceAttribute(ClassInterfaceType.AutoDual)] 
public class ObjectHandle : MarshalByRefObject, IObjectHandle
[ComVisibleAttribute(true)] 
[ClassInterfaceAttribute(ClassInterfaceType::AutoDual)] 
public ref class ObjectHandle : public MarshalByRefObject, IObjectHandle
/** @attribute ComVisibleAttribute(true) */ 
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.AutoDual) */ 
public class ObjectHandle extends MarshalByRefObject implements IObjectHandle
ComVisibleAttribute(true) 
ClassInterfaceAttribute(ClassInterfaceType.AutoDual) 
public class ObjectHandle extends MarshalByRefObject implements IObjectHandle

설명

ObjectHandle 클래스를 사용하면 ObjectHandle이 통과하는 각 AppDomain에 있는 래핑된 개체의 메타데이터를 로드하지 않고 여러 응용 프로그램 도메인 간에 래핑된 개체를 전달할 수 있습니다. 따라서 ObjectHandle 클래스를 사용하면 호출자는 원격 개체의 Type이 도메인에 로드되는 때를 제어할 수 있습니다.

예제

다음 코드 예제에서는 다른 AppDomain에 개체를 만들고 ObjectHandle에서 개체에 대한 프록시를 검색하는 방법을 보여 줍니다. 이 예제에서는 MyType 클래스의 코드가 "ObjectHandleAssembly"라는 어셈블리로 컴파일됩니다.

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 
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());
   }
}
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() );
   
}

상속 계층 구조

System.Object
   System.MarshalByRefObject
    System.Runtime.Remoting.ObjectHandle

스레드로부터의 안전성

이 형식의 모든 public static(Visual Basic의 경우 Shared) 멤버는 스레드로부터 안전합니다. 인터페이스 멤버는 스레드로부터 안전하지 않습니다.

플랫폼

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

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

참고 항목

참조

ObjectHandle 멤버
System.Runtime.Remoting 네임스페이스