从 C# 8 中的 System.Reflection.Metadata 获取接口可为 null 的上下文元数据



我目前正在制作一个工具来处理新的 C# 8 可为空的上下文。基本上是我的项目的API批准工具。

我正在使用System.Reflection.Metadata,只是在一种情况下提取元数据时遇到问题。

public class DerivedNullableBase2 : MyBase<MyBase<string?>?>
{
}

我正在尝试为我正在创建的 API 生成器工具获取 C#8 的可空上下文。因此,对于上述类,将生成以下 IL:

.class auto ansi nested public beforefieldinit DerivedNullableInterface2
extends [netstandard]System.Object
implements class [netstandard]System.Collections.Generic.IEnumerable`1<class [netstandard]System.Collections.Generic.IEnumerable`1<string>>,
[netstandard]System.Collections.IEnumerable
{
.interfaceimpl type class [netstandard]System.Collections.Generic.IEnumerable`1<class [netstandard]System.Collections.Generic.IEnumerable`1<string>>
.custom instance void System.Runtime.CompilerServices.NullableAttribute::.ctor(uint8[]) = ( 01 00 03 00 00 00 00 00 02 00 00 ) 
} // end of class DerivedNullableInterface2

我想提取其中包含的 NullableAttribute 以确定通用接口的可为空上下文。

我试图通过TypeDefinition和InterfaceImplementation获取属性,但似乎不存在,在这种情况下我将如何提取该NullableAttribute?

以下代码将使用 C# 8 语法从InterfaceImplementation中提取NullableAttribute

using var peReader = new PEReader(File.OpenRead(Assembly.GetExecutingAssembly().Location));
var mdReader = peReader.GetMetadataReader();
foreach (var attributeHandle in mdReader.CustomAttributes)
{
var attribute = mdReader.GetCustomAttribute(attributeHandle);
var ctorHandle = attribute.Constructor;
EntityHandle attributeTypeHandle = ctorHandle.Kind switch
{
HandleKind.MethodDefinition => mdReader.GetMethodDefinition((MethodDefinitionHandle)ctorHandle).GetDeclaringType(),
HandleKind.MemberReference => mdReader.GetMemberReference((MemberReferenceHandle)ctorHandle).Parent,
_ => throw new InvalidOperationException(),
};
StringHandle attributeTypeNameHandle = attributeTypeHandle.Kind switch
{
HandleKind.TypeDefinition => mdReader.GetTypeDefinition((TypeDefinitionHandle)attributeTypeHandle).Name,
HandleKind.TypeReference => mdReader.GetTypeReference((TypeReferenceHandle)attributeTypeHandle).Name,
_ => throw new InvalidOperationException(),
};
if (mdReader.StringComparer.Equals(attributeTypeNameHandle, "NullableAttribute"))
{
Console.WriteLine(attribute.Parent.Kind);
}
}

这是由 https://github.com/dotnet/corefx/issues/40234#issuecomment-520254880 提供的

最新更新