-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathIHasCustomAttributeExtensions.cs
More file actions
104 lines (87 loc) · 3.94 KB
/
Copy pathIHasCustomAttributeExtensions.cs
File metadata and controls
104 lines (87 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Diagnostics.CodeAnalysis;
using AsmResolver;
using AsmResolver.DotNet;
using AsmResolver.DotNet.Signatures;
using WindowsRuntime.Generator;
namespace WindowsRuntime.InteropGenerator;
/// <summary>
/// Extensions for <see cref="IHasCustomAttribute"/>.
/// </summary>
internal static class IHasCustomAttributeExtensions
{
/// <summary>
/// Tries to get an attribute that matches a particular type from a given metadata member.
/// </summary>
/// <param name="member">The metadata member.</param>
/// <param name="attributeType">The attribute type to look for.</param>
/// <param name="attribute">The resulting attribute, if found.</param>
/// <returns>Whether <paramref name="attribute"/> was successfully retrieved.</returns>
public static bool TryGetCustomAttribute(this IHasCustomAttribute member, ITypeDescriptor attributeType, [NotNullWhen(true)] out CustomAttribute? attribute)
{
for (int i = 0; i < member.CustomAttributes.Count; i++)
{
CustomAttribute currentAttribute = member.CustomAttributes[i];
// Skip invalid cases and error scenarios (shouldn't happen under normal conditions)
if (currentAttribute.Type is null)
{
continue;
}
// Check that the attribute type is a match
if (SignatureComparer.IgnoreVersion.Equals(currentAttribute.Type, attributeType))
{
attribute = currentAttribute;
return true;
}
}
attribute = null;
return false;
}
/// <summary>
/// Tries to get an attribute that matches a particular type from a given metadata member.
/// </summary>
/// <param name="member">The metadata member.</param>
/// <param name="ns">The namespace of the attribute type.</param>
/// <param name="name">The name of the attribute type.</param>
/// <param name="attribute">The resulting attribute, if found.</param>
/// <returns>Whether <paramref name="attribute"/> was successfully retrieved.</returns>
public static bool TryGetCustomAttribute(this IHasCustomAttribute member, string? ns, string? name, [NotNullWhen(true)] out CustomAttribute? attribute)
{
foreach (CustomAttribute currentAttribute in member.FindCustomAttributes(ns, name))
{
attribute = currentAttribute;
return true;
}
attribute = null;
return false;
}
/// <summary>
/// Tries to get an attribute that matches a particular type from a given metadata member.
/// </summary>
/// <param name="member">The metadata member.</param>
/// <param name="ns">The namespace of the attribute type.</param>
/// <param name="name">The name of the attribute type.</param>
/// <param name="attribute">The resulting attribute, if found.</param>
/// <returns>Whether <paramref name="attribute"/> was successfully retrieved.</returns>
public static bool TryGetCustomAttribute(this IHasCustomAttribute member, Utf8String? ns, Utf8String? name, [NotNullWhen(true)] out CustomAttribute? attribute)
{
foreach (CustomAttribute currentAttribute in member.FindCustomAttributes(ns, name))
{
attribute = currentAttribute;
return true;
}
attribute = null;
return false;
}
/// <summary>
/// Determines whether a metadata member is assigned an attribute that match a particular type.
/// </summary>
/// <param name="member">The metadata member.</param>
/// <param name="attributeType">The attribute type to look for.</param>
/// <returns>Whether <paramref name="member"/> has an attribute with the specified type.</returns>
public static bool HasCustomAttribute(this IHasCustomAttribute member, ITypeDescriptor attributeType)
{
return TryGetCustomAttribute(member, attributeType, out _);
}
}