MarshalByRefObject クラス
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
リモート処理をサポートするアプリケーションのアプリケーション ドメイン境界を越えてオブジェクトにアクセスできるようにします。
public ref class MarshalByRefObject abstract
public abstract class MarshalByRefObject
[System.Serializable]
public abstract class MarshalByRefObject
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class MarshalByRefObject
type MarshalByRefObject = class
[<System.Serializable>]
type MarshalByRefObject = class
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type MarshalByRefObject = class
Public MustInherit Class MarshalByRefObject
- 継承
-
MarshalByRefObject
- 派生
- 属性
例
このセクションには、2 つのコード例が含まれています。 最初のコード例は、別のアプリケーション ドメインでクラスのインスタンスを作成する方法を示しています。 2 番目のコード例は、リモート処理に使用できる単純なクラスを示しています。
例 1
次のコード例は、別のアプリケーション ドメインでコードを実行する最も簡単な方法を示しています。 この例では、Workerを継承する MarshalByRefObject という名前のクラスを定義し、実行しているアプリケーション ドメインの名前を表示するメソッドを使用します。 この例では、既定のアプリケーション ドメインと新しいアプリケーション ドメインに Worker のインスタンスを作成します。
Note
Workerを含むアセンブリは両方のアプリケーション ドメインに読み込む必要がありますが、新しいアプリケーション ドメインにのみ存在する他のアセンブリを読み込むことができます。
using namespace System;
using namespace System::Reflection;
public ref class Worker : MarshalByRefObject
{
public:
void PrintDomain()
{
Console::WriteLine("Object is executing in AppDomain \"{0}\"",
AppDomain::CurrentDomain->FriendlyName);
}
};
void main()
{
// Create an ordinary instance in the current AppDomain
Worker^ localWorker = gcnew Worker();
localWorker->PrintDomain();
// Create a new application domain, create an instance
// of Worker in the application domain, and execute code
// there.
AppDomain^ ad = AppDomain::CreateDomain("New domain");
Worker^ remoteWorker = (Worker^) ad->CreateInstanceAndUnwrap(
Worker::typeid->Assembly->FullName,
"Worker");
remoteWorker->PrintDomain();
}
/* This code produces output similar to the following:
Object is executing in AppDomain "source.exe"
Object is executing in AppDomain "New domain"
*/
using System;
using System.Reflection;
public class CreateInstanceWorker : MarshalByRefObject
{
public void PrintDomain()
{
Console.WriteLine("Object is executing in AppDomain \"{0}\"",
AppDomain.CurrentDomain.FriendlyName);
}
}
class CreateInstanceAndUnwrapSourceSnippet
{
public static void Main()
{
// Create an ordinary instance in the current AppDomain
CreateInstanceWorker localWorker = new CreateInstanceWorker();
localWorker.PrintDomain();
// Create a new application domain, create an instance
// of Worker in the application domain, and execute code
// there.
AppDomain ad = AppDomain.CreateDomain("New domain");
CreateInstanceWorker remoteWorker = (CreateInstanceWorker) ad.CreateInstanceAndUnwrap(
typeof(CreateInstanceWorker).Assembly.FullName,
"Worker");
remoteWorker.PrintDomain();
}
}
/* This code produces output similar to the following:
Object is executing in AppDomain "source.exe"
Object is executing in AppDomain "New domain"
*/
open System
open System.Reflection
type Worker() =
inherit MarshalByRefObject()
member _.PrintDomain() =
printfn $"Object is executing in AppDomain \"{AppDomain.CurrentDomain.FriendlyName}\""
// Create an ordinary instance in the current AppDomain
let localWorker = Worker()
localWorker.PrintDomain()
// Create a new application domain, create an instance
// of Worker in the application domain, and execute code
// there.
let ad = AppDomain.CreateDomain "New domain"
let remoteWorker =
ad.CreateInstanceAndUnwrap(typeof<Worker>.Assembly.FullName, "Worker") :?> Worker
remoteWorker.PrintDomain()
// This code produces output similar to the following:
// Object is executing in AppDomain "source.exe"
// Object is executing in AppDomain "New domain"
Imports System.Reflection
Public Class Worker
Inherits MarshalByRefObject
Public Sub PrintDomain()
Console.WriteLine("Object is executing in AppDomain ""{0}""", _
AppDomain.CurrentDomain.FriendlyName)
End Sub
End Class
Class Example
Public Shared Sub Main()
' Create an ordinary instance in the current AppDomain
Dim localWorker As New Worker()
localWorker.PrintDomain()
' Create a new application domain, create an instance
' of Worker in the application domain, and execute code
' there.
Dim ad As AppDomain = AppDomain.CreateDomain("New domain")
Dim remoteWorker As Worker = CType( _
ad.CreateInstanceAndUnwrap( _
GetType(Worker).Assembly.FullName, _
"Worker"), _
Worker)
remoteWorker.PrintDomain()
End Sub
End Class
' This code produces output similar to the following:
'
'Object is executing in AppDomain "source.exe"
'Object is executing in AppDomain "New domain"
例 2
次の例では、リモート処理で後で使用する MarshalByRefObject から派生したクラスを示します。
using namespace System;
using namespace System::Runtime::Remoting;
using namespace System::Security::Permissions;
public ref class SetObjectUriForMarshalTest
{
public:
ref class TestClass: public MarshalByRefObject{};
[SecurityPermissionAttribute(SecurityAction::Demand, Flags=SecurityPermissionFlag::RemotingConfiguration)]
static void Main()
{
TestClass^ obj = gcnew TestClass;
RemotingServices::SetObjectUriForMarshal( obj, "testUri" );
RemotingServices::Marshal(obj);
Console::WriteLine( RemotingServices::GetObjectUri( obj ) );
}
};
using System;
using System.Runtime.Remoting;
public class SetObjectUriForMarshalTest {
class TestClass : MarshalByRefObject {
}
public static void Main() {
TestClass obj = new TestClass();
RemotingServices.SetObjectUriForMarshal(obj, "testUri");
RemotingServices.Marshal(obj);
Console.WriteLine(RemotingServices.GetObjectUri(obj));
}
}
open System
open System.Runtime.Remoting
open System.Security.Permissions
type TestClass() =
inherit MarshalByRefObject()
[<EntryPoint>]
let main _ =
let obj = TestClass()
RemotingServices.SetObjectUriForMarshal(obj, "testUri")
RemotingServices.Marshal obj |> ignore
printfn $"{RemotingServices.GetObjectUri obj}"
0
Imports System.Runtime.Remoting
Imports System.Security.Permissions
Public Class SetObjectUriForMarshalTest
Class TestClass
Inherits MarshalByRefObject
End Class
<SecurityPermission(SecurityAction.Demand, Flags:= SecurityPermissionFlag.RemotingConfiguration )> _
Public Shared Sub Main()
Dim obj As TestClass = New TestClass()
RemotingServices.SetObjectUriForMarshal(obj, "testUri")
RemotingServices.Marshal(obj)
Console.WriteLine(RemotingServices.GetObjectUri(obj))
End Sub
End Class
注釈
アプリケーション ドメインは、1 つ以上のアプリケーションが存在するオペレーティング システム プロセスのパーティションです。 同じアプリケーション ドメイン内のオブジェクトは直接通信します。 異なるアプリケーション ドメイン内のオブジェクトは、アプリケーション ドメインの境界を越えてオブジェクトのコピーを転送するか、プロキシを使用してメッセージを交換することによって通信します。
MarshalByRefObject は、プロキシを使用してメッセージを交換することで、アプリケーション ドメインの境界を越えて通信するオブジェクトの基本クラスです。 MarshalByRefObjectから継承されないオブジェクトは、値によって暗黙的にマーシャリングされます。 リモート アプリケーションが値によるマーシャリング オブジェクトを参照する場合、オブジェクトのコピーはアプリケーション ドメインの境界を越えて渡されます。
MarshalByRefObject オブジェクトは、ローカル アプリケーション ドメインの境界内で直接アクセスされます。 リモート アプリケーション ドメイン内のアプリケーションが初めて MarshalByRefObjectにアクセスすると、プロキシがリモート アプリケーションに渡されます。 プロキシの後続の呼び出しは、ローカル アプリケーション ドメインに存在するオブジェクトにマーシャリングされます。
型は、アプリケーション ドメインの境界を越えて使用される場合は MarshalByRefObject から継承する必要があり、オブジェクトのメンバーは作成されたアプリケーション ドメインの外部では使用できないため、オブジェクトの状態をコピーすることはできません。
アプリケーション ドメインの境界を越えて使用するために MarshalByRefObject からオブジェクトを派生させる場合は、そのメンバーをオーバーライドしたり、そのメソッドを直接呼び出したりしないでください。 ランタイムは、 MarshalByRefObject から派生したクラスをアプリ ドメインの境界を越えてマーシャリングする必要があることを認識します。
コンストラクター
| 名前 | 説明 |
|---|---|
| MarshalByRefObject() |
MarshalByRefObject クラスの新しいインスタンスを初期化します。 |
メソッド
| 名前 | 説明 |
|---|---|
| CreateObjRef(Type) |
リモート オブジェクトとの通信に使用されるプロキシの生成に必要なすべての関連情報を含むオブジェクトを作成します。 |
| Equals(Object) |
指定したオブジェクトが現在のオブジェクトと等しいかどうかを判断します。 (継承元 Object) |
| GetHashCode() |
既定のハッシュ関数として機能します。 (継承元 Object) |
| GetLifetimeService() |
古い.
このインスタンスの有効期間ポリシーを制御する現在の有効期間サービス オブジェクトを取得します。 |
| GetType() |
現在のインスタンスの Type を取得します。 (継承元 Object) |
| InitializeLifetimeService() |
古い.
このインスタンスの有効期間ポリシーを制御する有効期間サービス オブジェクトを取得します。 |
| MemberwiseClone() |
現在の Objectの簡易コピーを作成します。 (継承元 Object) |
| MemberwiseClone(Boolean) |
現在の MarshalByRefObject オブジェクトの簡易コピーを作成します。 |
| ToString() |
現在のオブジェクトを表す文字列を返します。 (継承元 Object) |