内容简介:平时我们获取一个程序集或者类型的本文介绍如何为仅反射加载的程序集读取 Attribute 元数据信息。使用
平时我们获取一个程序集或者类型的 Attribute
是非常轻松的,只需要通过 GetCustomAttribute
方法就能拿到实例然后获取其中的值。但是,有时我们仅为反射加载一些程序集的时候,获取这些元数据就不那么简单了,因为我们没有加载目标程序集中的类型。
本文介绍如何为仅反射加载的程序集读取 Attribute 元数据信息。
仅反射加载一个程序集
使用 ReflectionOnlyLoadFrom
可以仅以反射的方式加载一个程序集。
var extensionFilePath = @"C:\Users\walterlv\Desktop\Walterlv.Extension.dll"; var assembly = Assembly.ReflectionOnlyLoadFrom(extensionFilePath);
获取程序集的 Attribute(例如获取程序集版本号)
Assembly.GetCustomAttributesData()
得到的是一个 CustomAttributeData
的列表,而这个列表中的每一项都与普通反射中拿到的特性集合不同,这里拿到的只是特性的信息(以下循环中的 data
变量)。
CustomAttributeData
中有 AttributeType
属性,虽然此属性是 Type
类型的,但是实际上它只会是 RuntimeType
类型,而不会是真实的 Attribute
的类型(因为不能保证宿主程序域中已经加载了那个类型)。
var customAttributesData = assembly.GetCustomAttributesData(); foreach (CustomAttributeData data in customAttributesData) { // 这里可以针对每一个拿到的慝的信息进行操作。 }
比如我们要获取这个程序集的版本号,正常我们写 assembly.GetCustomAttribute<AssemblyFileVersionAttribute>().Version
,但是这里我们无法生成 AssemblyFileVersionAttribute
的实例,我们只能这么写:
var versionString = assembly.GetCustomAttributesData() .FirstOrDefault(x => x.AttributeType.FullName == typeof(AssemblyFileVersionAttribute).FullName) ?.ConstructorArguments[0].Value as string ?? "0.0"; var version = new Version(versionString);
代码解读是这样的:
-
我们从拿到的所有的
Attribute
元数据中找到第一个名称与AssemblyFileVersionAttribute
相同的数据; -
从数据的构造函数参数中找到传入的参数值,而这个值就是我们定义
AssemblyFileVersionAttribute
时传入的参数的实际值。
因为我们知道 AssemblyFileVersionAttribute
的构造函数只有一个,所以我们确信可以从第一个参数中拿到我们想要的值。
顺便一提,我们使用 AssemblyFileVersionAttribute
而不是使用 AssemblyVersionAttribute
是因为使用 .NET Core 新格式(基于 Microsoft.NET.Sdk)编译出来的程序集默认是不带 AssemblyVersionAttribute
的。详见: 语义版本号(Semantic Versioning) - walterlv
。
参考资料
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- Go语言反射之反射调用
- Go语言反射之类型反射
- Go语言反射之值反射
- 模块讲解----反射 (基于web路由的反射)
- 装饰器与元数据反射(4)元数据反射
- .NET/C# 反射的的性能数据,以及高性能开发建议(反射获取 Attribute 和反射调用方法)
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Responsive Web Design
Ethan Marcotte / Happy Cog / 2011-6 / USD 18.00
From mobile browsers to netbooks and tablets, users are visiting your sites from an increasing array of devices and browsers. Are your designs ready? Learn how to think beyond the desktop and craft be......一起来看看 《Responsive Web Design》 这本书的介绍吧!