C#反射在实际应用中的事例代码

反射提供了封装程序集、模块和类型的对象(Type 类型)。可以使用反射动态创建类型的实例,将类型绑定到现有对象,或从现有对象获取类型并调用其方法或访问其字段和属性。如果代码中使用了属性,可以利用反射对它们进行访问。

下面我就以一个事例来说明反射在项目中的使用方法。

大体分为三个步骤:

第一步,在web.config配置如下代码(目的是为了动态的去修改所需分析的dll)

[c-sharp]  view plain copy
  1. <appSettings>  
  2.     <add key="BizAssembly" value="PSMS.Biz"/>  
  3. </appSettings>  

第二步,定义一个用于处理公共程序集的类

[c-sharp]  view plain copy
  1. /// <summary>  
  2.     /// 完成从客户端获取远程业务逻辑对象的代理  
  3.     /// </summary>  
  4.     public static class FacadeService  
  5.     {  
  6.         static IDictionary<string, Type> serviceClassCatalog;//定义一个键值对接口对象  
  7.         static FacadeService()  
  8.         {  
  9.             serviceClassCatalog = new Dictionary<string, Type>();  
  10.             Assembly assembly = Assembly.Load(new AssemblyName(ConfigurationManager.AppSettings["BizAssembly"]));//开始加载程序集对象  
  11.             Type[] types = assembly.GetExportedTypes();//获取程序集中所有对象的类型集合  
  12.             Type baseType = typeof(MarshalByRefObject);  
  13.             foreach (Type type in types)  
  14.             {  
  15.                 if (baseType.IsAssignableFrom(type))  
  16.                 {  
  17.                     Type[] interfaces = type.GetInterfaces();  
  18.                     //此处登记的是接口类型最终派生的接口类型,即最高层接口  
  19.                     if (interfaces.Length > 0)  
  20.                     {  
  21.                         serviceClassCatalog.Add(interfaces[0].FullName, type);  
  22.                     }  
  23.                 }  
  24.             }  
  25.         }  
  26.   
  27.         /// <summary>  
  28.         /// 根据传入的业务逻辑类的接口类型,返回实现该接口的类型对象实例远程代理  
  29.         /// </summary>  
  30.         /// <typeparam name="IFacade">具体的业务逻辑接口类型</typeparam>  
  31.         /// <returns>实现该接口的类型对象实例远程代理</returns>  
  32.         public static IFacade GetFacade<IFacade>()  
  33.         {  
  34.             string typeName = typeof(IFacade).FullName;  
  35.             if (serviceClassCatalog.ContainsKey(typeName))  
  36.             {  
  37.                 object realProxy = Activator.CreateInstance(serviceClassCatalog[typeName]);  
  38.                 return (IFacade)realProxy;  
  39.             }  
  40.             else  
  41.             {  
  42.                 throw new Exception("未包含接口所定义的服务类型。");  
  43.             }  
  44.         }  
  45.     }  

第三步,在程序代码中实现调用

[c-sharp]  view plain copy
  1. public partial class MyTest: System.Web.UI.Page  
  2. {  
  3.    //在后台代码中构建一个(测试用的)接口的实例对象  
  4.     static IUserInfoFacade userInfoFacade = FacadeService.GetFacade<IUserInfoFacade>();  
  5.    //其它功能实现代码  
  6.    //......  
  7.    //......  
  8.    private void Method1()  
  9.    {  
  10.        //具体的调用  
  11.        List<UserInfo> lstUserInfo = userInfoFacade.GetUserInfoList(unitCode, 0, 0);  
  12.        //其它功能实现代码  
  13.         //......  
  14.        //......  
  15.    }  
  16. }  

转载于:https://www.cnblogs.com/zhangchenliang/archive/2013/03/20/2970701.html

猜你喜欢

转载自blog.csdn.net/weixin_34232617/article/details/93495350