欢迎来到天天文库
浏览记录
ID:9240734
大小:37.69 KB
页数:12页
时间:2018-04-24
《csharp扩展方法和扩展属性》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、CSharp扩展方法扩展方法是实现新增类方法同时不改变类名称的一种技术。可以部分的替代原本必须继承类才能够实现的功能。但是,目前还不能扩展类的属性,也不能在不继承类的情况下而直接修改类方法。例如在Asp.netMVCWebApplication中的,我们想快速了解某个Action上是否有某个Attribute.那我们可以使用这样的扩展方法://////Getsthemethod.//////Type///Theinstance./
2、//Themethodselector.///MethodInfopublicstaticMethodInfoGetMethod(thisTinstance,Expression>methodSelector){//itisnotworkallmethodreturn((MethodCallExpression)methodSelector.Body).Method;}//////Getsthemethod.///3、ary>//////Theinstance.///Themethodselector.///MethodInfopublicstaticMethodInfoGetMethod(thisTinstance,Expression>methodSelector){return((MethodCallExpression)methodSele4、ctor.Body).Method;}//////Determineswhetherthespecifiedmemberhasattribute.//////Thetypeoftheattribute.///Themember.//////trueifthespecifiedmemberhasattribute;otherwise,false.///p5、ublicstaticboolHasAttribute(thisMemberInfomember)whereTAttribute:Attribute{returnGetAttributes(member).Length>0;}//////Getstheattributes.//////Thetypeoftheattribute.///Themember.///6、rns>publicstaticTAttribute[]GetAttributes(thisMemberInfomember)whereTAttribute:Attribute{varattributes=member.GetCustomAttributes(typeof(TAttribute),true);return(TAttribute[])attributes;}如何使用,请看下面的代码,我们使用lambda表达式获取某个方法,然后获取其上面的Attribute:[Fact]publicvoidGetHttpPostAttributeF7、romCreateAction(){//Arrangevarcontroller=GetEmployeeController(newMemeoryEmployeeBoService());//ActboolhasPostAttribute=controller.GetMethod(e=>e.Create(newEmployee())).HasAttribute();//AssertAssert.
3、ary>//////Theinstance.///Themethodselector.///MethodInfopublicstaticMethodInfoGetMethod(thisTinstance,Expression>methodSelector){return((MethodCallExpression)methodSele
4、ctor.Body).Method;}//////Determineswhetherthespecifiedmemberhasattribute.//////Thetypeoftheattribute.///Themember.//////trueifthespecifiedmemberhasattribute;otherwise,false.///p
5、ublicstaticboolHasAttribute(thisMemberInfomember)whereTAttribute:Attribute{returnGetAttributes(member).Length>0;}//////Getstheattributes.//////Thetypeoftheattribute.///Themember.///6、rns>publicstaticTAttribute[]GetAttributes(thisMemberInfomember)whereTAttribute:Attribute{varattributes=member.GetCustomAttributes(typeof(TAttribute),true);return(TAttribute[])attributes;}如何使用,请看下面的代码,我们使用lambda表达式获取某个方法,然后获取其上面的Attribute:[Fact]publicvoidGetHttpPostAttributeF7、romCreateAction(){//Arrangevarcontroller=GetEmployeeController(newMemeoryEmployeeBoService());//ActboolhasPostAttribute=controller.GetMethod(e=>e.Create(newEmployee())).HasAttribute();//AssertAssert.
6、rns>publicstaticTAttribute[]GetAttributes(thisMemberInfomember)whereTAttribute:Attribute{varattributes=member.GetCustomAttributes(typeof(TAttribute),true);return(TAttribute[])attributes;}如何使用,请看下面的代码,我们使用lambda表达式获取某个方法,然后获取其上面的Attribute:[Fact]publicvoidGetHttpPostAttributeF
7、romCreateAction(){//Arrangevarcontroller=GetEmployeeController(newMemeoryEmployeeBoService());//ActboolhasPostAttribute=controller.GetMethod(e=>e.Create(newEmployee())).HasAttribute();//AssertAssert.
此文档下载收益归作者所有