简体   繁体   English

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

[英]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.

相关问题 无法从程序集System.Reflection.Metadata加载类型&#39;System.Reflection.Metadata.ISignatureTypeProvider - Could not load type 'System.Reflection.Metadata.ISignatureTypeProvider from assembly System.Reflection.Metadata 如何使用 System.Reflection.Metadata 查找实现特定接口的所有类 - How to find all classes that implement a specific interface using System.Reflection.Metadata 如何使用System.Reflection.Metadata获取属性类型? - How to get type of property using System.Reflection.Metadata? 尝试生成语义模型时无法加载文件或程序集&#39;System.Reflection.Metadata - Could not load file or assembly 'System.Reflection.Metadata while trying to generate Semantic Model 从图像中获取元数据c# - Getting metadata from an image c# C#反射获取可为空的类型 - C# reflection getting nullable type 在c#中使用元数据提取器获取自定义XMP元数据 - Getting custom XMP metadata using metadata-extractor in c# F#Struct with Interface,C#metadata不显示Interface的继承 - F# Struct with Interface, C# metadata does not show inheritance from Interface 如何从 System.Reflection.MethodBase.MetadataToken 获取 System.Reflection.Metadata.MethodDefintion? - How to get a System.Reflection.Metadata.MethodDefintion from System.Reflection.MethodBase.MetadataToken? C# 反射从接口获取混凝土 class 的 static 属性 - C# Reflection Getting static property of concrete class from Interface
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM