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
파생
특성

예제

이 섹션에는 두 코드 예제가 있습니다. 첫 번째 코드 예제에서는 다른 애플리케이션 도메인에서 클래스의 인스턴스를 만드는 방법을 보여 줍니다. 두 번째 코드 예제에서는 원격에 사용할 수 있는 간단한 클래스를 보여 있습니다.

예제 1

다음 코드 예제에는 다른 애플리케이션 도메인에서 코드를 실행 하는 가장 간단한 방법은 보여 줍니다. 이라는 클래스를 정의 하는 예제 Worker 상속 되는 MarshalByRefObject, 실행 중인 애플리케이션 도메인의 이름을 표시 하는 메서드를 사용 하 여 합니다. 인스턴스를 만듭니다 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

설명

애플리케이션 도메인은 하나 이상의 애플리케이션이 상주 하는 운영 체제 프로세스의 파티션입니다. 동일한 애플리케이션 도메인의 개체는 직접 통신합니다. 다른 애플리케이션 도메인의 개체에는 애플리케이션 도메인 경계를 넘어 개체의 복사본을 전송 하거나 메시지를 교환 하는 프록시를 사용 하 여 통신 합니다.

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)

적용 대상