値渡しでマーシャリングされるオブジェクト参照をラップし、それらの参照が間接参照を通じて返されるようにします。
この型のすべてのメンバの一覧については、ObjectHandle メンバ を参照してください。
System.Object
System.MarshalByRefObject
System.Runtime.Remoting.ObjectHandle
<ClassInterface(ClassInterfaceType.AutoDual)>
Public Class ObjectHandle Inherits MarshalByRefObject Implements IObjectHandle
[C#]
[ClassInterface(ClassInterfaceType.AutoDual)]
public class ObjectHandle : MarshalByRefObject, IObjectHandle
[C++]
[ClassInterface(ClassInterfaceType::AutoDual)]
public __gc class ObjectHandle : public MarshalByRefObject, IObjectHandle
[JScript]
public
ClassInterface(ClassInterfaceType.AutoDual)
class ObjectHandle extends MarshalByRefObject implements IObjectHandle
スレッドセーフ
この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。
解説
ObjectHandle クラスは、ラップされた状態のオブジェクトを複数のアプリケーション ドメイン間で渡すために使用され、このとき、ラップされているオブジェクトのメタデータをその ObjectHandle がたどる各 AppDomain に読み込む必要はありません。したがって、 ObjectHandle クラスを使用すると、リモート オブジェクトの Type をドメインにいつ読み込むかを呼び出し元が制御できるようになります。
使用例
[Visual Basic, C#, C++] 別の 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
[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());
}
}
[C++]
#using <mscorlib.dll>
using namespace System;
using namespace System::Runtime::Remoting;
public __gc class MyType : public MarshalByRefObject {
public:
MyType() {
Console::Write(S"Created an instance of MyType in an AppDomain with the ");
Console::WriteLine(S"hash code {0}", __box( AppDomain::CurrentDomain->GetHashCode()));
Console::WriteLine(S"");
}
public:
int GetAppDomainHashCode() {
return AppDomain::CurrentDomain->GetHashCode();
}
};
int main() {
Console::WriteLine(S"The hash code of the default AppDomain is {0}.",
__box( AppDomain::CurrentDomain->GetHashCode()));
Console::WriteLine(S"");
// Creates another AppDomain.
AppDomain* domain = AppDomain::CreateDomain(S"AnotherDomain",
0,
(AppDomainSetup*)0);
// Creates an instance of MyType defined in the assembly called ObjectHandleAssembly.
ObjectHandle* obj = domain->CreateInstance(S"ObjectHandleAssembly", S"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(S"The unwrapped object is a proxy.");
else
Console::WriteLine(S"The unwrapped object is not a proxy!");
Console::WriteLine(S"");
Console::Write(S"Calling a method on the object located in an AppDomain with the hash code ");
Console::WriteLine(testObj->GetAppDomainHashCode());
}
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン
をクリックします。
必要条件
名前空間: System.Runtime.Remoting
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ
アセンブリ: Mscorlib (Mscorlib.dll 内)