用法很簡單:
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
新聞熱點
疑難解答