c# - GetCustomAttribute 在 .NET5 上的运行时返回 null
问题描述
我有一个加载程序集并按属性对数据进行分类的代码。
我正在尝试获取实际的 CustomAttribute 对象。在网络框架上调试期间,代码返回一个值。在 .Net5 上运行代码返回 null
编辑 在文档中进行一些挖掘之后:
.NET Framework 2.0 版提供了一个新的加载上下文,即仅反射上下文,可用于检查无法加载执行的代码。
https://docs.microsoft.com/en-us/dotnet/framework/reflection-and-codedom/accessing-custom-attributes
编辑 #2 原因是我试图在运行时获取对象,并且如上面的文档引用中所写,无法加载属性以及任何其他运行时仅反射上下文,并且该方法返回 null。
除了使用 system.reflection 在运行时获取实际属性对象之外,还有其他解决方案吗?
字典初始化
var ass = Assembly.LoadFile(Path.GetFullPath("MyInterface.dll"));
var asss = Assembly.GetExecutingAssembly();
var apiInterfaces = ass.DefinedTypes.Where(x => x.IsInterface && x.CustomAttributes != null && x.CustomAttributes.Any(z => z.AttributeType.FullName != null && z.AttributeType.FullName.Equals(typeof(ApiInterfaceDescriptorAttribute).FullName)));
属性:
[AttributeUsage(AttributeTargets.Interface)]
public class ApiInterfaceDescriptorAttribute : Attribute
{
public string ApiUsageName { get; }
public ApiInterfaceDescriptorAttribute(string apiUsageName)
{
ApiUsageName = apiUsageName;
}
}
示例界面
[ApiInterfaceDescriptor("powercontrol")]
public interface IMyInterface
{
[ApiMethodDescriptor(ApiExposureLevel.Basic, "a-method...")]
void SomeMethod();
}
获得属性的试炼
public class ApiDetector
{
private Dictionary<Type, List<MethodInfo>> _apiDictionary = new Dictionary<Type, List<MethodInfo>>();
public void LoadApiElements()
{
var apiKeyDesriptor = key.GetCustomAttribute(typeof(ApiInterfaceDescriptorAttribute)) as ApiInterfaceDescriptorAttribute;
_apiDetector.Add(new ApiDataObjectDescriptor { Name = key.Name, ApiUsageName = apiKeyDesriptor?.ApiUsageName, Type = key });
}
}
在 .Net5 远程机器上运行代码返回 null:
欢迎任何帮助。提前谢谢!
解决方案
根本原因是对 Assembly.LoadFile 的调用。此方法始终将指定的程序集加载到它自己的 AssemblyLoadContext 中。(有关程序集加载上下文的更多信息)。
这意味着您已将程序集加载到 LoadFile 加载上下文(一个副本)中,并且我从您的主代码中直接引用它,这会将其加载到默认加载上下文(第二个副本)中。虽然程序集文件完全相同,但对于运行时,这两个副本是不同的程序集,并且两者中的所有类型都将被视为不同的类型。
将程序集加载方法更改为:
Assembly.GetAssembly(typeof(MyType));
已解决问题。
感谢 Microsoft 支持团队的 vitek-karas。
推荐阅读
- python - 在 matplotlib 中为任何艺术家制作动画的一般方法?
- python - 是否有等效的“sum()”内置函数使用增强赋值?
- java - 出乎意料的 VarHandle 性能(比替代方案慢 4 倍)
- javascript - 表单上的输入值不显示 HTML WordPress
- android - 隐藏/显示片段中的导航元素
- javascript - 如何识别移动设备中的地址栏?
- c - 在汇编中使用 fgets 时出现分段错误错误?
- python - COCO python API已安装但无法导入
- python - TensorFlow 模型的成本始终为 0
- stm32 - STM32F303 发现 - USART_IT_RXNE 从不接收 antyhing(无 DMA/无 HAL)