1 回答

TA貢獻1851條經驗 獲得超3個贊
以下代碼將從使用 C# 8 語法NullableAttribute中提取 a InterfaceImplementation。
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);
? ? }
}
- 1 回答
- 0 關注
- 192 瀏覽
添加回答
舉報