MarshalByRefObject Osztály

Definíció

Lehetővé teszi az objektumok elérését az alkalmazás tartományhatárai között az újraegyezést támogató alkalmazásokban.

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
Öröklődés
MarshalByRefObject
Származtatott
Attribútumok

Példák

Ez a szakasz két példakódot tartalmaz. Az első példakód bemutatja, hogyan hozhat létre egy osztálypéldányt egy másik alkalmazástartományban. A második példakód egy egyszerű osztályt mutat be, amely újraírásra használható.

Példa 1

Az alábbi példakód a kód egy másik alkalmazástartományban való végrehajtásának legegyszerűbb módját mutatja be. A példa egy öröklő Workerosztályt MarshalByRefObject határoz meg egy metódussal, amely annak az alkalmazástartománynak a nevét jeleníti meg, amelyben a végrehajtást hajtja végre. A példa az alapértelmezett alkalmazástartományban és egy új alkalmazástartományban hozza létre a példányokat Worker .

Note

A tartalmazott Worker szerelvényt mindkét alkalmazástartományba be kell tölteni, de más szerelvényeket is betölthet, amelyek csak az új alkalmazástartományban léteznek.

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. példa

Az alábbi példa egy olyan osztályt mutat be, amelyből MarshalByRefObject később használatos az újraegyezés során.

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

Megjegyzések

Az alkalmazástartomány egy olyan operációsrendszer-folyamat partíciója, amelyben egy vagy több alkalmazás található. Az ugyanabban az alkalmazástartományban lévő objektumok közvetlenül kommunikálnak. A különböző alkalmazástartományokban lévő objektumok vagy az objektumok másolatának az alkalmazástartomány határain való átvitelével, vagy egy proxy használatával kommunikálnak az üzenetek cseréjéhez.

MarshalByRefObject az alkalmazástartomány határain keresztül kommunikáló objektumok alaposztálya proxy használatával történő üzenetváltással. Azok az objektumok, amelyektől nem öröklődnek MarshalByRefObject , implicit módon érték szerint vannak megadva. Ha egy távoli alkalmazás egy marshal by value objektumra hivatkozik, a rendszer átadja az objektum másolatát az alkalmazás tartományhatárai között.

MarshalByRefObject az objektumok közvetlenül a helyi alkalmazástartomány határain belül érhetők el. Amikor egy távoli alkalmazástartománybeli alkalmazás első alkalommal fér hozzá egy MarshalByRefObjectproxyhoz, a rendszer átadja a proxyt a távoli alkalmazásnak. A proxy későbbi hívásait a rendszer visszaállítja a helyi alkalmazástartományban található objektumra.

A típusoknak öröklődniük kell attól az időszaktól MarshalByRefObject , amikor a típust az alkalmazás tartományhatárai között használják, és az objektum állapotát nem szabad másolni, mert az objektum tagjai nem használhatók azon az alkalmazástartományon kívül, ahol létrehozták őket.

Ha egy objektumot MarshalByRefObject az alkalmazás tartományhatárai közötti használatra hoz létre, ne bírálja felül egyik tagját sem, és ne hívja meg közvetlenül a metódusait. A futtatókörnyezet felismeri, hogy a származtatott MarshalByRefObject osztályokat az alkalmazás tartományhatárai között kell futtatni.

Konstruktorok

Name Description
MarshalByRefObject()

Inicializálja a MarshalByRefObject osztály új példányát.

Metódusok

Name Description
CreateObjRef(Type)

Létrehoz egy objektumot, amely tartalmazza a távoli objektumokkal való kommunikációhoz használt proxy létrehozásához szükséges összes releváns információt.

Equals(Object)

Meghatározza, hogy a megadott objektum egyenlő-e az aktuális objektummal.

(Öröklődés forrása Object)
GetHashCode()

Ez az alapértelmezett kivonatoló függvény.

(Öröklődés forrása Object)
GetLifetimeService()
Elavult.

Lekéri a példány élettartamszabályzatát vezérlő aktuális élettartam-szolgáltatásobjektumot.

GetType()

Lekéri az Type aktuális példányt.

(Öröklődés forrása Object)
InitializeLifetimeService()
Elavult.

Beolvas egy élettartam-szolgáltatásobjektumot a példány élettartam-szabályzatának szabályozásához.

MemberwiseClone()

Az aktuális Objectpéldány sekély másolatát hozza létre.

(Öröklődés forrása Object)
MemberwiseClone(Boolean)

Az aktuális MarshalByRefObject objektum sekély másolatát hozza létre.

ToString()

Az aktuális objektumot jelképező sztringet ad vissza.

(Öröklődés forrása Object)

A következőre érvényes: