国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

C#語法糖之 ReflectionSugar 通用反射類

2019-11-17 02:16:28
字體:
供稿:網(wǎng)友

C#語法糖之 ReflectionSugar 通用反射類

用法很簡單:

ReflectionSugar rs = new ReflectionSugar(100);//緩存100秒 ,可以不填默認(rèn)不緩存

rs.有嘛點嘛

性能測試:

性能測試類源碼

http://www.survivalescaperooms.com/sunkaixuan/p/4540840.html

using SyntacticSugar;using System;using System.Collections.Generic;using System.Linq;using System.Reflection;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;namespace Test.IO{    public partial class ReflectionTest : System.Web.UI.Page    {        PRotected void Page_Load(object sender, EventArgs e)        {            //性能測試類            PerformanceTest p = new PerformanceTest();            p.SetCount(100000);//循環(huán)次數(shù)(默認(rèn):1)            p.SetIsMultithread(false);//是否啟動多線程測試 (默認(rèn):false)            /******************************CreateInstance********************************/            //帶cache            p.Execute(            i =>            {                CreateInstanceByCache();//調(diào)用函數(shù)            },            message => { Response.Write(message); });   //總共執(zhí)行時間:0.09901秒            //不帶cache            p.Execute(              i =>              {                  CreateInstanceNoCache();//調(diào)用函數(shù)              },             message => { Response.Write(message); });   //總共執(zhí)行時間:0.32002秒            /******************************ExecuteMethod********************************/            //帶cache            p.Execute(            i =>            {                ExecuteMethodByCache();//調(diào)用函數(shù)            },            message => { Response.Write(message); });   //總共執(zhí)行時間:0.36202秒            //不帶cache            p.Execute(              i =>              {                  ExecuteMethodNoCache();//調(diào)用函數(shù)              },             message => { Response.Write(message); });   //總共執(zhí)行時間:1.35508秒            /******************************ExecuteMethod********************************/            //帶cache            p.Execute(            i =>            {                ExecuteMethodByCache();//調(diào)用函數(shù)            },            message => { Response.Write(message); });   //總共執(zhí)行時間:0.36202秒            //不帶cache            p.Execute(              i =>              {                  ExecuteMethodNoCache();//調(diào)用函數(shù)              },             message => { Response.Write(message); });   //總共執(zhí)行時間:1.35508秒            /******************************LoadFile********************************/            //帶cache            p.Execute(            i =>            {                 LoadFileByCache();//調(diào)用函數(shù)            },            message => { Response.Write(message); });   //總共執(zhí)行時間:0.11801            //不帶cache            p.Execute(              i =>              {                  LoadFileNoCache();//調(diào)用函數(shù)              },             message => { Response.Write(message); });   //總共執(zhí)行時間:4.89628秒            //還有其它方法就不測試了        }        //獲取實列        private static void CreateInstanceByCache()        {            ReflectionSugar rs = new ReflectionSugar(100);//緩存100秒            var f = rs.CreateInstance<FileSugar>("SyntacticSugar.FileSugar", "SyntacticSugar");        }        //獲取實列        private static void CreateInstanceNoCache()        {            string path = "SyntacticSugar.FileSugar,SyntacticSugar";//命名空間.類型名,程序集            Type o = Type.GetType(path);//加載類型            FileSugar obj = (FileSugar)Activator.CreateInstance(o, true);//根據(jù)類型創(chuàng)建實例        }        //執(zhí)行函數(shù)        private static void ExecuteMethodByCache()        {            ReflectionSugar rs = new ReflectionSugar(100);//緩存100秒            var path = rs.ExecuteMethod("SyntacticSugar", "FileSugar", "GetMapPath", "~/");        }        //執(zhí)行函數(shù)        private static void ExecuteMethodNoCache()        {            ReflectionSugar rs = new ReflectionSugar(0);//緩存0秒            var path = rs.ExecuteMethod("SyntacticSugar", "FileSugar", "GetMapPath", "~/");        }        //加載程序集        private static void LoadFileByCache()        {            ReflectionSugar rs = new ReflectionSugar(100);//緩存100秒            Assembly ams = rs.LoadFile(@"D:/學(xué)習(xí)/SyntacticSugar/SyntacticSugar/bin/Debug/SyntacticSugar.dll");        }        //加載程序集        private static void LoadFileNoCache()        {            ReflectionSugar rs = new ReflectionSugar(0);//緩存100秒            Assembly ams = rs.LoadFile(@"D:/學(xué)習(xí)/SyntacticSugar/SyntacticSugar/bin/Debug/SyntacticSugar.dll");        }    }}

  

反射類源碼:

using System;using System.Collections.Generic;using System.Linq;using System.Reflection;using System.Text;using System.Web;using System.Web.Caching;namespace SyntacticSugar{    /// <summary>    /// ** 描述:反射通用類    /// ** 創(chuàng)始時間:2010-2-28    /// ** 修改時間:-    /// ** 修改人:sunkaixuan    /// ** 使用說明: http://www.survivalescaperooms.com/sunkaixuan/p/4635710.html    /// </summary>    public class ReflectionSugar    {        public static int Minutes = 60;        public static int Hour = 60 * 60;        public static int Day = 60 * 60 * 24;        private int _time = 0;        private bool _isCache { get { return _time > 0; } }        /// <summary>        /// 緩存時間,0為不緩存(默認(rèn)值:0秒,單位:秒)        /// </summary>        public ReflectionSugar(int time = 0)        {            _time = time;        }        /// <summary>        /// 創(chuàng)建對象實例        /// </summary>        /// <typeparam name="T"></typeparam>        /// <param name="fullName">命名空間.類型名</param>        /// <param name="assemblyName">程序集(dll名稱)</param>        /// <returns></returns>        public T CreateInstance<T>(string fullName, string assemblyName)        {            string key = GetKey("CreateInstance1", fullName, assemblyName);            if (_isCache)                if (ContainsKey(key))                {                    return Get<T>(key);                }            string path = fullName + "," + assemblyName;//命名空間.類型名,程序集            Type o = Type.GetType(path);//加載類型            object obj = Activator.CreateInstance(o, true);//根據(jù)類型創(chuàng)建實例            var reval = (T)obj;            if (_isCache)                Add<T>(key, reval, _time);            return reval;//類型轉(zhuǎn)換并返回        }        /// <summary>        /// 創(chuàng)建對象實例        /// </summary>        /// <typeparam name="T">要創(chuàng)建對象的類型</typeparam>        /// <param name="assemblyName">類型所在程序集名稱(dll名稱)</param>        /// <param name="nameSpace">類型所在命名空間</param>        /// <param name="className">類型名</param>        /// <returns></returns>        public T CreateInstance<T>(string assemblyName, string nameSpace, string className)        {            string key = GetKey("CreateInstance2", assemblyName, nameSpace, className);            if (_isCache)                if (ContainsKey(key))                {                    return Get<T>(key);                }            try            {                string fullName = nameSpace + "." + className;//命名空間.類型名                //此為第一種寫法                object ect = Assembly.Load(assemblyName).CreateInstance(fullName);//加載程序集,創(chuàng)建程序集里面的 命名空間.類型名 實例                var reval = (T)ect;//類型轉(zhuǎn)換并返回                if (_isCache)                    Add<T>(key, reval, _time);                return reval;                //下面是第二種寫法                //string path = fullName + "," + assemblyName;//命名空間.類型名,程序集                //Type o = Type.GetType(path);//加載類型                //object obj = Activator.CreateInstance(o, true);//根據(jù)類型創(chuàng)建實例                //return (T)obj;//類型轉(zhuǎn)換并返回            }            catch            {                //發(fā)生異常,返回類型的默認(rèn)值                var reval = default(T);                if (_isCache)                    Add<T>(key, reval, _time);                return reval;//類型轉(zhuǎn)換            }        }        /// <summary>        /// 加載程序集        /// </summary>        /// <param name="path"></param>        /// <returns></returns>        public Assembly LoadFile(string path)        {            string key = GetKey("LoadFile", pat
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 林口县| 西丰县| 格尔木市| 通山县| 西吉县| 孙吴县| 简阳市| 桂林市| 彝良县| 崇信县| 榆社县| 阿克| 永德县| 靖边县| 玉屏| 香格里拉县| 塘沽区| 天水市| 轮台县| 蒙山县| 巴彦县| 罗定市| 布尔津县| 秭归县| 册亨县| 新源县| 石河子市| 临西县| 灵宝市| 江安县| 岢岚县| 山东| 延长县| 梁平县| 光山县| 河北省| 阜新| 江源县| 祁门县| 福建省| 固原市|