白开心

  IT博客 :: 首页 ::  :: 联系 :: 聚合  :: 管理 ::
  9 随笔 :: 76 文章 :: 28 评论 :: 0 Trackbacks
使用反射调用指定的服务器端程序集
Server端:
 1using System;
 2using System.Collections.Generic;
 3using System.Text;
 4using System.Runtime.Remoting.Channels.Tcp;
 5using System.Runtime.Remoting.Channels;
 6using System.Runtime.Remoting;
 7
 8namespace Server
 9{
10    class Program
11    {
12        private static TcpServerChannel channel = null;
13        private static int Port = 8080;
14
15        static void Main(string[] args)
16        {
17            channel = new TcpServerChannel("NcZerg", Port);
18            ChannelServices.RegisterChannel(channel, false);
19            System.Type ServerObj = typeof(ServerObjects.ServerObjFactory);
20            /// System.Type ServerObj = typeof(ServerObjects.ServerObject);
21            System.Runtime.Remoting.WellKnownServiceTypeEntry Services = new WellKnownServiceTypeEntry(ServerObj, "Services", WellKnownObjectMode.SingleCall);
22            RemotingConfiguration.RegisterWellKnownServiceType(Services);
23            Console.WriteLine("服务已经启动");
24            Console.ReadLine();
25        }

26    }

27}

Client端:

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using IServerObj;

namespace Client
{
    
class RemotingServices
    
{
        
private static IServerObjFactory _ServerFactory = null;

        
private static IServerObjFactory ServerFactory
        
{
            
get
            
{
                
if (_ServerFactory == null)
                
{
                    _ServerFactory 
= (IServerObjFactory)Activator.GetObject(
                        
typeof(IServerObjFactory),
                        
"tcp://127.0.0.1:8080/Services");
                }


                
return _ServerFactory;
            }

        }


        
private static IServerObject _ServerObject = null;
        
public static IServerObject ServerObject
        
{
            
get
            
{
                
if (_ServerObject == null)
                
{
                    
/// 利用反射和工厂方法类构造自己所需的实例类
                    _ServerObject = (IServerObject)ServerFactory.GetInstance("ServerObjects.ServerObject");
 
                }

                
return _ServerObject;
            }

        }

    }

}



应用程序类:
IServerObj工程

 1using System;
 2using System.Collections.Generic;
 3using System.Text;
 4
 5namespace IServerObj
 6{
 7    public interface IServerObject
 8    {
 9        int GetSum(int a, int b);
10        void SayHello(String name);
11    }

12
13    public interface IServerObjFactory
14    {
15        IServerObject CreateInstance();
16        object GetInstance(String ClassName);
17    }

18}

19

ServerObjects工程:
 1using System;
 2using System.Collections.Generic;
 3using System.Text;
 4using IServerObj;
 5using System.Reflection;
 6
 7namespace ServerObjects
 8{
 9    [Serializable]
10    public class ServerObject : MarshalByRefObject, IServerObject
11    {
12
13        public int GetSum(int a, int b)
14        {
15            return a + b;
16        }

17
18        public void SayHello(String name)
19        {
20            ;
21        }

22    }

23
24    /// <summary>
25    /// 工厂方法,构件对象
26    /// </summary>

27    [Serializable]
28    public class ServerObjFactory : MarshalByRefObject, IServerObjFactory
29    {
30        public IServerObject CreateInstance()
31        {
32            return new ServerObject();
33        }

34
35        /// <summary>
36        /// 利用反射和工厂方法类构造自己所需的实例类
37        /// </summary>
38        /// <param name="ClassName"></param>
39        /// <returns></returns>

40        public object GetInstance(String ClassName)
41        {
42            Assembly assembly = Assembly.GetExecutingAssembly();
43            Type typForm = assembly.GetType(ClassName);
44            Object obj = typForm.InvokeMember(
45                    null,
46                    BindingFlags.DeclaredOnly |
47                    BindingFlags.Public | BindingFlags.NonPublic |
48                    BindingFlags.Instance | BindingFlags.CreateInstance,
49                    null,
50                    null,
51                    null);
52
53            return obj;
54        }

55    }

56}

57
posted on 2008-03-05 01:30 白开心 阅读(319) 评论(0)  编辑 收藏 引用 所属分类: .Net(学习ing...)
只有注册用户登录后才能发表评论。