- 1、本文档共14页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 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
您可能关注的文档
- 5年级上册作文备课.doc
- 第二章桩基础工程习题.doc
- 第二章试验室标准化与信息化管理题(基础).doc
- 5年级上册课本知识.doc
- 第二讲数列问题的题型与方法1.doc
- 第二课告别游戏成瘾(教案).doc
- 5年级下3单元备课2.doc
- 第五分册中国史—晚清史部分选择题.doc
- 第五章PowerPoint电子演示文稿单选题.doc
- 第五章微生物的代谢习题.doc
- 2024年证券分析与咨询服务项目投资申请报告代可行性研究报告.docx
- 2024年铬酸酐项目资金申请报告代可行性研究报告.docx
- 2024年清洁胶项目资金申请报告代可行性研究报告.docx
- 2024年肉松饼项目投资申请报告代可行性研究报告.docx
- 2024年陆上泵项目资金需求报告代可行性研究报告.docx
- 2024年未硫化复合橡胶及其制品项目资金需求报告代可行性研究报告.docx
- 2024年精密温控节能设备项目资金筹措计划书代可行性研究报告.docx
- 2024年汽车覆盖件模具项目资金筹措计划书代可行性研究报告.docx
- 宋词行书钢笔字帖.pdf
- 我的暑假生活作文三年级300字10篇.pdf
文档评论(0)