[英]Getting interface nullable context metadata from System.Reflection.Metadata in C# 8
I am making a tool to handle new C# 8 nullable contexts at the moment.我目前正在制作一个工具来处理新的 C# 8 可为空的上下文。 Basically a API approval tool for my projects.
基本上是我项目的 API 批准工具。
I am using System.Reflection.Metadata, just having problem extracting metadata in one case.我正在使用 System.Reflection.Metadata,只是在一种情况下提取元数据时遇到问题。
public class DerivedNullableBase2 : MyBase<MyBase<string?>?>
{
}
I am attempting to get the C#8 the nullable context for a API generator tool I am creating.我试图让 C#8 成为我正在创建的 API 生成器工具的可为空上下文。 So for the above class the following IL is generated:
因此,对于上述类,生成了以下 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
I want to extract the NullableAttribute contained within to determine the nullable context of the generic interfaces.我想提取其中包含的 NullableAttribute 以确定通用接口的可为空上下文。
I attempted to get the attribute both via the TypeDefinition and the InterfaceImplementation but doesn't appear to be there, how would I extract that NullableAttribute in this scenario?我试图通过 TypeDefinition 和 InterfaceImplementation 获取属性,但似乎不存在,在这种情况下我将如何提取 NullableAttribute?
The following code will extract a NullableAttribute
from a InterfaceImplementation
using C# 8 syntax.以下代码将使用 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);
}
}
This was provided by https://github.com/dotnet/corefx/issues/40234#issuecomment-520254880这是由https://github.com/dotnet/corefx/issues/40234#issuecomment-520254880提供的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.