亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C#反射在實(shí)際應(yīng)用中的實(shí)例代碼

 更新時(shí)間:2013年03月20日 16:29:37   作者:  
C#反射在實(shí)際應(yīng)用中的實(shí)例代碼,需要的朋友可以參考一下

反射提供了封裝程序集、模塊和類型的對(duì)象(Type 類型)??梢允褂梅瓷鋭?dòng)態(tài)創(chuàng)建類型的實(shí)例,將類型綁定到現(xiàn)有對(duì)象,或從現(xiàn)有對(duì)象獲取類型并調(diào)用其方法或訪問其字段和屬性。如果代碼中使用了屬性,可以利用反射對(duì)它們進(jìn)行訪問。

下面我就以一個(gè)事例來說明反射在項(xiàng)目中的使用方法。

大體分為三個(gè)步驟:

第一步,在web.config配置如下代碼(目的是為了動(dòng)態(tài)的去修改所需分析的dll)

復(fù)制代碼 代碼如下:

<appSettings> 
    <add key="BizAssembly" value="PSMS.Biz"/> 
</appSettings> 

第二步,定義一個(gè)用于處理公共程序集的類

復(fù)制代碼 代碼如下:

/// <summary> 
    /// 完成從客戶端獲取遠(yuǎn)程業(yè)務(wù)邏輯對(duì)象的代理 
    /// </summary> 
    public static class FacadeService 
    { 
        static IDictionary<string, Type> serviceClassCatalog;//定義一個(gè)鍵值對(duì)接口對(duì)象 
        static FacadeService() 
        { 
            serviceClassCatalog = new Dictionary<string, Type>(); 
            Assembly assembly = Assembly.Load(new AssemblyName(ConfigurationManager.AppSettings["BizAssembly"]));//開始加載程序集對(duì)象 
            Type[] types = assembly.GetExportedTypes();//獲取程序集中所有對(duì)象的類型集合 
            Type baseType = typeof(MarshalByRefObject); 
            foreach (Type type in types) 
            { 
                if (baseType.IsAssignableFrom(type)) 
                { 
                    Type[] interfaces = type.GetInterfaces(); 
                    //此處登記的是接口類型最終派生的接口類型,即最高層接口 
                    if (interfaces.Length > 0) 
                    { 
                        serviceClassCatalog.Add(interfaces[0].FullName, type); 
                    } 
                } 
            } 
        } 

        /// <summary> 
        /// 根據(jù)傳入的業(yè)務(wù)邏輯類的接口類型,返回實(shí)現(xiàn)該接口的類型對(duì)象實(shí)例遠(yuǎn)程代理 
        /// </summary> 
        /// <typeparam name="IFacade">具體的業(yè)務(wù)邏輯接口類型</typeparam> 
        /// <returns>實(shí)現(xiàn)該接口的類型對(duì)象實(shí)例遠(yuǎn)程代理</returns> 
        public static IFacade GetFacade<IFacade>() 
        { 
            string typeName = typeof(IFacade).FullName; 
            if (serviceClassCatalog.ContainsKey(typeName)) 
            { 
                object realProxy = Activator.CreateInstance(serviceClassCatalog[typeName]); 
                return (IFacade)realProxy; 
            } 
            else 
            { 
                throw new Exception("未包含接口所定義的服務(wù)類型。"); 
            } 
        } 
    } 

第三步,在程序代碼中實(shí)現(xiàn)調(diào)用

復(fù)制代碼 代碼如下:

public partial class MyTest: System.Web.UI.Page 

   //在后臺(tái)代碼中構(gòu)建一個(gè)(測(cè)試用的)接口的實(shí)例對(duì)象 
    static IUserInfoFacade userInfoFacade = FacadeService.GetFacade<IUserInfoFacade>(); 
   //其它功能實(shí)現(xiàn)代碼 
   //...... 
   //...... 
   private void Method1() 
   { 
       //具體的調(diào)用 
       List<UserInfo> lstUserInfo = userInfoFacade.GetUserInfoList(unitCode, 0, 0); 
       //其它功能實(shí)現(xiàn)代碼 
        //...... 
       //...... 
   } 

相關(guān)文章

最新評(píng)論