HOW TO:建立用戶端啟動型別的執行個體
本文說明兩種產生用戶端啟動物件的方式。第一個方法使用 CreateInstance,第二個方法則使用 new 運算子。
使用 Activator.CreateInstance 建立執行個體
建立並註冊 TcpChannel
Dim channel As TcpChannel = New TcpChannel() ChannelServices.RegisterChannel(channel, False) TcpChannel channel = new TcpChannel(); ChannelServices.RegisterChannel(channel, false);
註冊用戶端啟動物件
RemotingConfiguration.RegisterActivatedClientType( _ GetType(MyRemoteObject), _ "tcp://localhost:1234/MyServer") RemotingConfiguration.RegisterActivatedClientType( typeof(MyRemoteObject), "tcp://localhost:1234/MyServer");
呼叫 CreateInstance
Dim url() As Object = {New UrlAttribute("tcp://localhost:1234/Server")} Dim obj As MyRemoteObject = CType(Activator.CreateInstance( _ GetType(MyRemoteObject), _ Nothing, _ url), MyRemoteObject) object[] url = { new UrlAttribute("tcp://localhost:1234/Server") }; MyRemoteObject obj = (MyRemoteObject)Activator.CreateInstance( typeof(MyRemoteObject), null, url);
使用 New 運算子建立執行個體
建立並註冊通道
Dim channel As TcpChannel = New TcpChannel() ChannelServices.RegisterChannel(channel, False) TcpChannel channel = new TcpChannel(); ChannelServices.RegisterChannel(channel, false);
註冊用戶端啟動物件
RemotingConfiguration.RegisterActivatedClientType( _ GetType(MyRemoteObject), _ "tcp://localhost:1234/MyServer") RemotingConfiguration.RegisterActivatedClientType( typeof(MyRemoteObject), "tcp://localhost:1234/MyServer");
呼叫 new 運算子
Dim obj As MyRemoteObject = New MyRemoteObject(123) MyRemoteObject obj = new MyRemoteObject(123);
範例
下列程式碼範例示範兩種用來建立用戶端啟動執行個體的方法:
Imports System
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp
Imports System.Runtime.Remoting.Activation
Imports Server
Module Client
Sub Main()
' Create and register a channel
Dim channel As TcpChannel = New TcpChannel()
ChannelServices.RegisterChannel(channel, False)
' Register the client activated object
RemotingConfiguration.RegisterActivatedClientType( _
GetType(MyRemoteObject), _
"tcp://localhost:1234/MyServer")
' Call Activator.CreateInstance
Dim obj As MyRemoteObject = CType(Activator.CreateInstance( _
GetType(MyRemoteObject), _
Nothing, _
url), MyRemoteObject)
' OR call operator new
Dim obj As MyRemoteObject = New MyRemoteObject(123)
Console.WriteLine("Client.Main(): GetValue returned: {0}", obj.GetValue())
Console.WriteLine("Client.Main(): Calling SetValue(10)")
obj.SetValue(10)
Console.WriteLine("Client.Main(): GetValue returned: {0}", obj.GetValue())
End Sub
End Module
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Activation;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using Server;
namespace Client
{
class Client
{
static void Main(string[] args)
{
// Create and register channel
TcpChannel channel = new TcpChannel();
ChannelServices.RegisterChannel(channel, false);
// Register client activated object
RemotingConfiguration.RegisterActivatedClientType(
typeof(MyRemoteObject),
"tcp://localhost:1234/MyServer");
// Call Activator.CreateInstance
object[] url = { new UrlAttribute("tcp://localhost:1234/Server") };
MyRemoteObject obj = (MyRemoteObject)Activator.CreateInstance(
typeof(MyRemoteObject),
null,
url);
// OR call operator new
MyRemoteObject obj = new MyRemoteObject(123);
Console.WriteLine("Client.Main(): GetValue returned: " + obj.GetValue());
Console.WriteLine("Client.Main(): Calling SetValue(10)");
obj.SetValue(10);
Console.WriteLine("Client.Main(): GetValue returned: " + obj.GetValue());
}
}
}
編譯程式碼
這個範例需要:
- System 和 System.Runtime.Remoting 命名空間的參考,以及可實作 MyRemoteObject 的命名空間。
請參閱
概念
遠端物件的啟動過程
遠端應用程式的組態
伺服器啟動過程
存留期租用期
用戶端啟動過程
Copyright © 2007 by Microsoft Corporation. All rights reserved.