C#Emit实现动态代理:配置信息保存及实现.doc

C#Emit实现动态代理:配置信息保存及实现.doc

  1. 1、本文档共14页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
我实现这个动态代理的初衷是,我自己编写了一个配置信息库,可以将配置信息保存到特定的文件中,提供加密的选项,保证数据不会被恶意的读取,但是面对多个应用,不同的配置项带来大量的重复编码工作。 以下是基础配置的读取和设置函数 public object setValue(string key, object v); public object getValue(string key); 针对某个网站应用的配置类为: public class WebConfig: ConfigBase { public string WebServer { get{return getValue(“Server”) as string;} set{setValue(“Server”,value); } public int Port { get{return getValue(Port) as int;} set{setValue(Port,value); } public DateTime LastAcessDate { get{return getValue(“LastAccess”) as string;} set{setValue(“LastAccess”,value); } } 代码很繁琐,而且很多重复的代码,这是没有什么意义的,所以我考虑用动态代理实现这个类,最终的配置信息变成: public interface WebConfig { string WebServer{get;set;} int Port{get;set;} DateTime LastAccessDate{get;set;} } … WebConfig a=ConfigBase.getConfigItem(typeof(WebConfig)); a.WebServer=”ssss”; 在getConfigItem中返回的实体类中包含了配置信息的读写操作,注意WebConfig没有继承任何基础接口;没有任何类实现了WebConfig的接口函数和属性,嗯,就是能工作,关键就在于Emit生成动态的WebConfig的继承类的实例,然后返回这个实例;同时监视这个实例的函数调用并读写配置文件。 配置实例 public TypedAppConfig() ??????? { ??????????? m_configurationType = typeof(T);//T template parameter ??????????? m_strSection=m_configurationType.Name+_configuration; ??????????? m_configurationObject=DynamicProxyFactory.CreateProxy( ??????????????? DynamicProxyFactory.CreateImplementT(), ??????????????? delegate(object target ??????????????????? , System.Reflection.MethodBase method ??????????????????? , object[] parameters) ??????????????? { ??????????????????? //Only listen get and set functionality ??????????????????? string key = method.Name; ??????????????????? if (key.StartsWith(get_)) ??????????????????? { ??????????????????????? key = key.Substring(get_.Length); ??????????????????????? return DataTypesCopy.FromClass((method as MethodInfo).ReturnType) ??????????????????????????? .CastValue( this.GetValue(m_strSection,key)); ??????????????????? } ??????????????????? else if (key.StartsWith(set_)) ??????????????????? { ??????????????????????? key = key.Substring(set_.Length); ??????????????????????? this.SetValue(m_strSection,key,param

文档评论(0)

wuyoujun92 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档