分享
 
 
 

C#语法糖之 ReflectionSugar 通用反射类

王朝学院·作者佚名  2016-05-20
窄屏简体版  字體: |||超大  

C#语法糖之 ReflectionSugar 通用反射类用法很简单:

ReflectionSugar rs = new ReflectionSugar(100);//缓存100秒 ,可以不填默认不缓存

rs.有嘛点嘛

性能测试:

性能测试类源码:

http://www.cnblogs.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);//循环次数(默认:1) p.SetIsMultithread(false);//是否启动多线程测试 (默认:false) /******************************CreateInstance********************************/ //带cache p.Execute( i => { CreateInstanceByCache();//调用函数 }, message => { Response.Write(message); }); //总共执行时间:0.09901秒 //不带cache p.Execute( i => { CreateInstanceNoCache();//调用函数 }, message => { Response.Write(message); }); //总共执行时间:0.32002秒 /******************************ExecuteMethod********************************/ //带cache p.Execute( i => { ExecuteMethodByCache();//调用函数 }, message => { Response.Write(message); }); //总共执行时间:0.36202秒 //不带cache p.Execute( i => { ExecuteMethodNoCache();//调用函数 }, message => { Response.Write(message); }); //总共执行时间:1.35508秒 /******************************ExecuteMethod********************************/ //带cache p.Execute( i => { ExecuteMethodByCache();//调用函数 }, message => { Response.Write(message); }); //总共执行时间:0.36202秒 //不带cache p.Execute( i => { ExecuteMethodNoCache();//调用函数 }, message => { Response.Write(message); }); //总共执行时间:1.35508秒 /******************************LoadFile********************************/ //带cache p.Execute( i => { LoadFileByCache();//调用函数 }, message => { Response.Write(message); }); //总共执行时间:0.11801 //不带cache p.Execute( i => { LoadFileNoCache();//调用函数 }, message => { Response.Write(message); }); //总共执行时间: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);//根据类型创建实例 } //执行函数 private static void ExecuteMethodByCache() { ReflectionSugar rs = new ReflectionSugar(100);//缓存100秒 var path = rs.ExecuteMethod("SyntacticSugar", "FileSugar", "GetMapPath", "~/"); } //执行函数 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:\学习\SyntacticSugar\SyntacticSugar\bin\Debug\SyntacticSugar.dll"); } //加载程序集 private static void LoadFileNoCache() { ReflectionSugar rs = new ReflectionSugar(0);//缓存100秒 Assembly ams = rs.LoadFile(@"D:\学习\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> /// ** 描述:反射通用类 /// ** 创始时间:2010-2-28 /// ** 修改时间:- /// ** 修改人:sunkaixuan /// ** 使用说明: http://www.cnblogs.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为不缓存(默认值:0秒,单位:秒) /// </summary> public ReflectionSugar(int time = 0) { _time = time; } /// <summary> /// 创建对象实例 /// </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);//根据类型创建实例 var reval = (T)obj; if (_isCache) Add<T>(key, reval, _time); return reval;//类型转换并返回 } /// <summary> /// 创建对象实例 /// </summary> /// <typeparam name="T">要创建对象的类型</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);//加载程序集,创建程序集里面的 命名空间.类型名 实例 var reval = (T)ect;//类型转换并返回 if (_isCache) Add<T>(key, reval, _time); return reval; //下面是第二种写法 //string path = fullName + "," + assemblyName;//命名空间.类型名,程序集 //Type o = Type.GetType(path);//加载类型 //object obj = Activator.CreateInstance(o, true);//根据类型创建实例 //return (T)obj;//类型转换并返回 } catch { //发生异常,返回类型的默认值 var reval = default(T); if (_isCache) Add<T>(key, reval, _time); return reval;//类型转换 } } /// <summary> /// 加载程序集 /// </summary> /// <param name="path"></param> /// <returns></returns> public Assembly LoadFile(string path) { string key = GetKey("LoadFile", pat

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有