日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > asp.net >内容正文

asp.net

AspectCore.Extension.Reflection : .NET Core反射扩展库

發(fā)布時(shí)間:2023/12/4 asp.net 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 AspectCore.Extension.Reflection : .NET Core反射扩展库 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

在從零實(shí)現(xiàn)AOP的過(guò)程中,難免會(huì)需要大量反射相關(guān)的操作,雖然在.net 4.5+/.net core中反射的性能有了大幅的優(yōu)化,但為了追求極致性能,自己實(shí)現(xiàn)了部分反射的替代方案,包括構(gòu)造器調(diào)用、方法調(diào)用、字段讀寫(xiě),屬性讀寫(xiě)和特性讀取。在重構(gòu)時(shí),把反射擴(kuò)展操作封裝到單獨(dú)的項(xiàng)目AspectCore.Extension.Reflection中,以此方便自己和大家使用。

獲取AspectCore.Extension.Reflection

通過(guò)nuget獲取AspectCore.Extension.Reflection

? ?Install-Package AspectCore.Extensions.Reflection -pre

構(gòu)造器反射擴(kuò)展

提供ConstructorReflector作為構(gòu)造器反射擴(kuò)展的入口,使用方式類似System.Reflection.ConstructorInfo:

var constructorInfo = typeof(ConstructorFakes).GetTypeInfo().GetConstructor(new Type[0]);var reflector = constructorInfo.GetReflector();var instance = reflector.Invoke(args);

性能測(cè)試(Reflection為.NET Core提供的反射調(diào)用,Reflector為AspectCore.Extension.Reflection調(diào)用,Native為硬編碼調(diào)用,下同):

| ? ? Method | ? ? ? Mean | ? ? Error | ? ?StdDev | ? ?StdErr | ? ? ? ? ?Op/s | ?Gen 0 | Allocated ||----------- |-----------:|----------:|----------:|----------:|--------------:|-------:|----------:|| Reflection | 119.505 ns | 0.5146 ns | 0.4814 ns | 0.1243 ns | ? 8,367,831.8 | 0.0074 | ? ? ?24 B || ?Reflector | ? 8.990 ns | 0.0403 ns | 0.0377 ns | 0.0097 ns | 111,236,649.9 | 0.0076 | ? ? ?24 B || ? ? Native | ? 3.825 ns | 0.0620 ns | 0.0580 ns | 0.0150 ns | 261,404,148.5 | 0.0076 | ? ? ?24 B |

方法調(diào)用反射擴(kuò)展

提供MethodReflector作為方法反射擴(kuò)展的入口,使用方式類似System.Reflection.MethodInfo

var typeInfo = typeof(MethodFakes).GetTypeInfo();var method = typeInfo.GetMethod("Call");var refector = method.GetReflector(); refector.Invoke(instance,args);

性能測(cè)試:

| ? ? ? ? ? ? Method | ? ? ? ?Mean | ? ? Error | ? ?StdDev | ? ?StdErr | ? ? ? ? ? ?Op/s ||------------------- |------------:|----------:|----------:|----------:|----------------:|| ? ? ? ?Native_Call | ? 1.0473 ns | 0.0064 ns | 0.0050 ns | 0.0015 ns | ? 954,874,046.8 || ? ?Reflection_Call | ?91.9543 ns | 0.3540 ns | 0.3311 ns | 0.0855 ns | ? ?10,874,961.4 || ? ? Reflector_Call | ? 7.1544 ns | 0.0628 ns | 0.0587 ns | 0.0152 ns | ? 139,774,408.3 |

屬性調(diào)用反射擴(kuò)展

提供PropertyReflector作為屬性反射擴(kuò)展的入口,使用方式類似System.Reflection.PropertyInfo

var property ?= typeof(PropertyFakes).GetTypeInfo().GetProperty("Property");var reflector = property.GetReflector();var value = reflector.GetValue(instance);

性能測(cè)試:

| ? ? ? ? ? ? ? ? ? ?Method | ? ? ? Mean | ? ? Error | ? ?StdDev | ? ?StdErr | ? ? ? ? ?Op/s | ?Gen 0 | Allocated ||-------------------------- |-----------:|----------:|----------:|----------:|--------------:|-------:|----------:|| ? ? ? Native_Get_Property | ? 1.178 ns | 0.0244 ns | 0.0229 ns | 0.0059 ns | 848,858,716.1 | ? ? ?- | ? ? ? 0 B || ? Reflection_Get_Property | 103.028 ns | 0.2217 ns | 0.2074 ns | 0.0535 ns | ? 9,706,088.1 | ? ? ?- | ? ? ? 0 B || ? ?Reflector_Get_Property | ? 4.172 ns | 0.0194 ns | 0.0172 ns | 0.0046 ns | 239,694,827.7 | ? ? ?- | ? ? ? 0 B || ? ? ? Native_Set_Property | ? 2.002 ns | 0.0122 ns | 0.0114 ns | 0.0030 ns | 499,447,543.5 | ? ? ?- | ? ? ? 0 B || ? Reflection_Set_Property | 188.313 ns | 0.5347 ns | 0.5002 ns | 0.1292 ns | ? 5,310,298.0 | 0.0203 | ? ? ?64 B || ? ?Reflector_Set_Property | ? 5.878 ns | 0.0234 ns | 0.0219 ns | 0.0056 ns | 170,138,324.7 | ? ? ?- | ? ? ? 0 B |

特性獲取擴(kuò)展

以獲取方法上標(biāo)記的特性為例。
方法定義如下:

[Attribute1][Attribute2("benchmark", Id = 10000)][Attribute3][Attribute3][Attribute3]public void Method(){}

使用MethodReflector來(lái)獲取特性:

var method = type.GetMethod("Method");var reflector = method.GetReflector();var attribute1 = reflector.GetCustomAttribute(typeof(Attribute1));var attributes = reflector.GetCustomAttributes();

性能測(cè)試:

| ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Method | ? ? ? ?Mean | ? ? ? ? Op/s | ?Gen 0 | Allocated ||-------------------------------------------- |------------:|-------------:|-------:|----------:|| ? ? ? ? ? ? ? Reflection_GetCustomAttribute | 4,642.13 ns | ? ?215,418.5 | 0.2289 | ? ? 744 B || ? ? ? ? ? ? ? ?Reflector_GetCustomAttribute | ? ?35.52 ns | 28,154,302.3 | 0.0101 | ? ? ?32 B || Reflection_GetCustomAttributes_WithAttrType | 5,354.49 ns | ? ?186,759.2 | 0.3281 | ? ?1048 B || ?Reflector_GetCustomAttributes_WithAttrType | ? 168.61 ns | ?5,930,816.1 | 0.0710 | ? ? 224 B || ? ? ? ? ?Reflection_GetCustomAttributes_All | 7,915.45 ns | ? ?126,335.2 | 0.5035 | ? ?1632 B || ? ? ? ? ? Reflector_GetCustomAttributes_All | ? ?98.36 ns | 10,166,253.6 | 0.0737 | ? ? 232 B || ? ? ? ? ? ? ? ? ? ? ? ?Reflection_IsDefined | 1,723.30 ns | ? ?580,283.6 | 0.0801 | ? ? 256 B || ? ? ? ? ? ? ? ? ? ? ? ? Reflector_IsDefined | ? ?35.55 ns | 28,126,759.1 | ? ? ?- | ? ? ? 0 B |

可以看到AspectCore.Extension.Reflection在性能上都比反射有2個(gè)數(shù)量級(jí)的優(yōu)化,達(dá)到了和硬編碼調(diào)用相同的數(shù)量級(jí)。尤其是獲取特性的優(yōu)化最為明顯。
可以在Github獲取詳細(xì)的性能測(cè)試。
Github :?https://github.com/dotnetcore/AspectCore-Framework

原文地址:http://www.cnblogs.com/liuhaoyang/p/aspectcore_extension_reflection.html


.NET社區(qū)新聞,深度好文,微信中搜索dotNET跨平臺(tái)或掃描二維碼關(guān)注

總結(jié)

以上是生活随笔為你收集整理的AspectCore.Extension.Reflection : .NET Core反射扩展库的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。