MarshalByRefObject Kelas
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Memungkinkan akses ke objek di seluruh batas domain aplikasi dalam aplikasi yang mendukung jarak jauh.
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
- Warisan
-
MarshalByRefObject
- Turunan
- Atribut
Contoh
Bagian ini berisi dua contoh kode. Contoh kode pertama menunjukkan cara membuat instans kelas di domain aplikasi lain. Contoh kode kedua menunjukkan kelas sederhana yang dapat digunakan untuk jarak jauh.
Contoh 1
Contoh kode berikut menunjukkan cara paling sederhana untuk menjalankan kode di domain aplikasi lain. Contoh mendefinisikan kelas bernama Worker yang mewarisi MarshalByRefObject, dengan metode yang menampilkan nama domain aplikasi tempatnya dijalankan. Contoh membuat instans Worker di domain aplikasi default dan di domain aplikasi baru.
Note
Rakitan yang berisi Worker harus dimuat ke kedua domain aplikasi, tetapi dapat memuat rakitan lain yang hanya akan ada di domain aplikasi baru.
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"
Contoh 2
Contoh berikut menunjukkan kelas yang berasal dari MarshalByRefObject yang digunakan nanti dalam jarak jauh.
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
Keterangan
Domain aplikasi adalah partisi dalam proses sistem operasi tempat satu atau beberapa aplikasi berada. Objek dalam domain aplikasi yang sama berkomunikasi secara langsung. Objek di domain aplikasi yang berbeda berkomunikasi baik dengan mengangkut salinan objek di seluruh batas domain aplikasi, atau dengan menggunakan proksi untuk bertukar pesan.
MarshalByRefObject adalah kelas dasar untuk objek yang berkomunikasi di seluruh batas domain aplikasi dengan bertukar pesan menggunakan proksi. Objek yang tidak diwarisi secara MarshalByRefObject implisit marshal menurut nilai. Ketika aplikasi jarak jauh mereferensikan marshal menurut objek nilai, salinan objek diteruskan di seluruh batas domain aplikasi.
MarshalByRefObject objek diakses langsung dalam batas domain aplikasi lokal. Pertama kali aplikasi di domain aplikasi jarak jauh mengakses MarshalByRefObject, proksi diteruskan ke aplikasi jarak jauh. Panggilan berikutnya pada proksi dinamai kembali ke objek yang berada di domain aplikasi lokal.
Jenis harus mewarisi dari MarshalByRefObject ketika jenis digunakan di seluruh batas domain aplikasi, dan status objek tidak boleh disalin karena anggota objek tidak dapat digunakan di luar domain aplikasi tempat objek dibuat.
Ketika Anda mendapatkan objek dari MarshalByRefObject untuk digunakan di seluruh batas domain aplikasi, Anda tidak boleh mengambil alih salah satu anggotanya, juga tidak boleh memanggil metodenya secara langsung. Runtime mengenali bahwa kelas yang berasal dari MarshalByRefObject harus dirusak di seluruh batas domain aplikasi.
Konstruktor
| Nama | Deskripsi |
|---|---|
| MarshalByRefObject() |
Menginisialisasi instans baru dari kelas MarshalByRefObject. |
Metode
| Nama | Deskripsi |
|---|---|
| CreateObjRef(Type) |
Membuat objek yang berisi semua informasi relevan yang diperlukan untuk menghasilkan proksi yang digunakan untuk berkomunikasi dengan objek jarak jauh. |
| Equals(Object) |
Menentukan apakah objek yang ditentukan sama dengan objek saat ini. (Diperoleh dari Object) |
| GetHashCode() |
Berfungsi sebagai fungsi hash default. (Diperoleh dari Object) |
| GetLifetimeService() |
Kedaluwarsa.
Mengambil objek layanan seumur hidup saat ini yang mengontrol kebijakan seumur hidup untuk instans ini. |
| GetType() |
Mendapatkan Type instans saat ini. (Diperoleh dari Object) |
| InitializeLifetimeService() |
Kedaluwarsa.
Mendapatkan objek layanan seumur hidup untuk mengontrol kebijakan seumur hidup untuk instans ini. |
| MemberwiseClone() |
Membuat salinan dangkal dari Objectsaat ini. (Diperoleh dari Object) |
| MemberwiseClone(Boolean) |
Membuat salinan dangkal objek MarshalByRefObject saat ini. |
| ToString() |
Mengembalikan string yang mewakili objek saat ini. (Diperoleh dari Object) |