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 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

설명

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

MarshalByRefObject 는 프록시를 사용하여 메시지를 교환하여 애플리케이션 도메인 경계를 넘어 통신하는 개체의 기본 클래스입니다. MarshalByRefObject를 상속하지 않는 개체는 암시적으로 값으로 마샬링되어집니다. 원격 애플리케이션이 값 개체로 마샬링을 참조하는 경우 개체의 복사본이 애플리케이션 도메인 경계를 넘어 전달됩니다.

MarshalByRefObject 개체는 로컬 애플리케이션 도메인의 경계 내에서 직접 액세스됩니다. 원격 애플리케이션 도메인의 애플리케이션이 처음으로 액세스하면 MarshalByRefObject프록시가 원격 애플리케이션에 전달됩니다. 프록시에 대한 후속 호출은 로컬 애플리케이션 도메인에 있는 개체로 다시 마샬링됩니다.

형식은 애플리케이션 도메인 경계를 MarshalByRefObject 넘어 형식을 사용할 때 상속해야 하며 개체의 멤버를 만든 애플리케이션 도메인 외부에서 사용할 수 없으므로 개체의 상태를 복사해서는 안 됩니다.

애플리케이션 도메인 경계를 MarshalByRefObject 넘어 사용하기 위해 개체를 파생하는 경우 해당 멤버를 재정의해서는 안 되며 메서드를 직접 호출해서는 안 됩니다. 런타임은 파생된 MarshalByRefObject 클래스가 앱 도메인 경계를 넘어 마샬링되어야 한다는 것을 인식합니다.

생성자

Name Description
MarshalByRefObject()

MarshalByRefObject 클래스의 새 인스턴스를 초기화합니다.

메서드

Name Description
CreateObjRef(Type)

원격 개체와 통신하는 데 사용되는 프록시를 생성하는 데 필요한 모든 관련 정보를 포함하는 개체를 만듭니다.

Equals(Object)

지정된 개체가 현재 개체와 같은지 여부를 확인합니다.

(다음에서 상속됨 Object)
GetHashCode()

기본 해시 함수로 사용됩니다.

(다음에서 상속됨 Object)
GetLifetimeService()
사용되지 않음.

이 인스턴스의 수명 정책을 제어하는 현재 수명 서비스 개체를 검색합니다.

GetType()

현재 인스턴스의 Type 가져옵니다.

(다음에서 상속됨 Object)
InitializeLifetimeService()
사용되지 않음.

이 인스턴스의 수명 정책을 제어하는 수명 서비스 개체를 가져옵니다.

MemberwiseClone()

현재 Object단순 복사본을 만듭니다.

(다음에서 상속됨 Object)
MemberwiseClone(Boolean)

현재 MarshalByRefObject 개체의 단순 복사본을 만듭니다.

ToString()

현재 개체를 나타내는 문자열을 반환합니다.

(다음에서 상속됨 Object)

적용 대상