MarshalByRefObject クラス

定義

リモート処理をサポートするアプリケーションで、アプリケーションのドメインの境界を越えてオブジェクトにアクセスできるようにします。

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

次のコード例は、別のアプリケーション ドメインでコードを実行する最も簡単な方法を示しています。 この例では、継承MarshalByRefObjectする名前Workerのクラスと、そのクラスが実行されているアプリケーション ドメインの名前を表示するメソッドを定義します。 この例では、既定の Worker アプリケーション ドメインと新しいアプリケーション ドメインにインスタンスを作成します。

注意

含まれているアセンブリは両方の 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 Worker : MarshalByRefObject
{
    public void PrintDomain()
    {
        Console.WriteLine("Object is executing in AppDomain \"{0}\"",
            AppDomain.CurrentDomain.FriendlyName);
    }
}

class Example
{
    public static void Main()
    {
        // Create an ordinary instance in the current AppDomain
        Worker localWorker = new 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(
            typeof(Worker).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)

適用対象