-
Notifications
You must be signed in to change notification settings - Fork 222
/
AttributeDataExtensions.cs
87 lines (82 loc) · 3.59 KB
/
AttributeDataExtensions.cs
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
/*
* SonarAnalyzer for .NET
* Copyright (C) 2015-2023 SonarSource SA
* mailto: contact AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
namespace SonarAnalyzer.Extensions
{
public static class AttributeDataExtensions
{
public static bool HasName(this AttributeData attribute, string name) =>
attribute is { AttributeClass.Name: { } attributeClassName } && attributeClassName == name;
public static bool HasAnyName(this AttributeData attribute, params string[] names) =>
names.Any(x => attribute.HasName(x));
public static bool TryGetAttributeValue<T>(this AttributeData attribute, string valueName, out T value)
{
// named arguments take precedence over constructor arguments of the same name. For [Attr(valueName: false, valueName = true)] "true" is returned.
if (attribute.NamedArguments.IndexOf(x => x.Key.Equals(valueName, StringComparison.OrdinalIgnoreCase)) is var namedAgumentIndex and >= 0)
{
return TryConvertConstant(attribute.NamedArguments[namedAgumentIndex].Value, out value);
}
else if (attribute.AttributeConstructor.Parameters.IndexOf(x => x.Name.Equals(valueName, StringComparison.OrdinalIgnoreCase)) is var constructorParameterIndex and >= 0)
{
return TryConvertConstant(attribute.ConstructorArguments[constructorParameterIndex], out value);
}
else
{
value = default;
return false;
}
}
public static bool HasAttributeUsageInherited(this AttributeData attribute) =>
attribute.AttributeClass.GetAttributes()
.Where(x => x.AttributeClass.Is(KnownType.System_AttributeUsageAttribute))
.SelectMany(x => x.NamedArguments.Where(x => x.Key == nameof(AttributeUsageAttribute.Inherited)))
.Where(x => x.Value is { Kind: TypedConstantKind.Primitive, Type.SpecialType: SpecialType.System_Boolean })
.Select(x => (bool?)x.Value.Value)
.FirstOrDefault() ?? true; // Default value of Inherited is true
private static bool TryConvertConstant<T>(TypedConstant constant, out T value)
{
value = default;
if (constant.IsNull)
{
return true;
}
else if (constant.Value is T result)
{
value = result;
return true;
}
else if (constant.Value is IConvertible)
{
try
{
value = (T)Convert.ChangeType(constant.Value, typeof(T));
return true;
}
catch
{
return false;
}
}
else
{
return false;
}
}
}
}