AspectCore.Extension.Reflection : .NET Core反射扩展库
来源:互联网 发布:taft地震波数据下载 编辑:程序博客网 时间:2024/06/07 16:44
在从零实现AOP的过程中,难免会需要大量反射相关的操作,虽然在.net 4.5+/.net core中反射的性能有了大幅的优化,但为了追求极致性能,自己实现了部分反射的替代方案,包括构造器调用、方法调用、字段读写,属性读写和特性读取。在重构时,把反射扩展操作封装到单独的项目AspectCore.Extension.Reflection中,以此方便自己和大家使用。
获取AspectCore.Extension.Reflection
通过nuget获取AspectCore.Extension.Reflection
Install-Package AspectCore.Extensions.Reflection -pre
构造器反射扩展
提供ConstructorReflector作为构造器反射扩展的入口,使用方式类似System.Reflection.ConstructorInfo:
var constructorInfo = typeof(ConstructorFakes).GetTypeInfo().GetConstructor(new Type[0]);var reflector = constructorInfo.GetReflector();var instance = reflector.Invoke(args);
性能测试(Reflection为.NET Core提供的反射调用,Reflector为AspectCore.Extension.Reflection调用,Native为硬编码调用,下同):
| 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 |
方法调用反射扩展
提供MethodReflector作为方法反射扩展的入口,使用方式类似System.Reflection.MethodInfo
var typeInfo = typeof(MethodFakes).GetTypeInfo();var method = typeInfo.GetMethod("Call");var refector = method.GetReflector();refector.Invoke(instance,args);
性能测试:
| 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 |
属性调用反射扩展
提供PropertyReflector作为属性反射扩展的入口,使用方式类似System.Reflection.PropertyInfo
var property = typeof(PropertyFakes).GetTypeInfo().GetProperty("Property");var reflector = property.GetReflector();var value = reflector.GetValue(instance);
性能测试:
| 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 |
特性获取扩展
以获取方法上标记的特性为例。
方法定义如下:
[Attribute1] [Attribute2("benchmark", Id = 10000)] [Attribute3] [Attribute3] [Attribute3] public void Method() { }
使用MethodReflector来获取特性:
var method = type.GetMethod("Method");var reflector = method.GetReflector();var attribute1 = reflector.GetCustomAttribute(typeof(Attribute1));var attributes = reflector.GetCustomAttributes();
性能测试:
| 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个数量级的优化,达到了和硬编码调用相同的数量级。尤其是获取特性的优化最为明显。
可以在Github获取详细的性能测试。
Github : https://github.com/dotnetcore/AspectCore-Framework
原文地址:http://www.cnblogs.com/liuhaoyang/p/aspectcore_extension_reflection.html
.NET社区新闻,深度好文,微信中搜索dotNET跨平台或扫描二维码关注
- AspectCore.Extension.Reflection : .NET Core反射扩展库
- Asp.Net Core轻量级Aop解决方案:AspectCore
- [Asp.Net Core轻量级Aop解决方案]AspectCore Project 介绍
- .NET Reflection optimization 反射优化
- .NET反射Reflection机制学习
- .NET 反射:反射发出(Reflection Emit)
- 推荐最新的Asp.Net Core 下开源的AOP框架 AspectCore
- .NET 类型扩展(Type Extension)
- Net反射(Reflection)学习笔记
- 反射(Reflection)
- 反射reflection
- 反射Reflection
- Reflection反射
- 反射 reflection
- 反射Reflection
- Reflection反射
- 反射reflection
- 反射(Reflection)
- ubuntu云服务器搭建jdk+mysql+tomcat
- keras框架
- 进程间通讯(一)管道
- nginx 支持thinkphp设置
- java.lang.ExceptionInInitializerError的原因
- AspectCore.Extension.Reflection : .NET Core反射扩展库
- spring cloud+dotnet core搭建微服务架构:配置中心续(五)
- Android自定义控件drawText的baseline的问题
- 解析各类文件--cookbook读书笔记
- 开发笔记——前端优化
- 整理springdata mongodb 查询 一
- 习题1
- Android 报错解决办法:I/O error during system call, Connection reset by peer
- 小结