本文介绍实例化客户端激活对象的两种方式。第一种方式使用 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 的命名空间的引用。
请参见
概念
激活远程对象
远程应用程序的配置
服务器激活
生存期租约
客户端激活
.gif)
版权所有 (C) 2007 Microsoft Corporation。保留所有权利。