-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
ILLinkLinkAttributes.cs
98 lines (85 loc) · 3.12 KB
/
ILLinkLinkAttributes.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
88
89
90
91
92
93
94
95
96
97
98
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Diagnostics.CodeAnalysis;
using BindingFlags = System.Reflection.BindingFlags;
class ILLinkLinkAttributes
{
public static int Run()
{
ThrowIfTypeNotPresent(typeof(ILLinkLinkAttributes), nameof(TestDontRemoveAttribute));
ThrowIfTypePresent(typeof(ILLinkLinkAttributes), nameof(TestRemoveAttribute));
ThrowIfTypePresent(typeof(ILLinkLinkAttributes), nameof(TestMarkAllRemoveAttribute));
ThrowIfAttributePresent(typeof(ILLinkLinkAttributes), nameof(_fieldWithCustomAttribute), nameof(TestRemoveAttribute));
ThrowIfAttributeNotPresent(typeof(ILLinkLinkAttributes), nameof(_fieldWithCustomAttribute), nameof(TestDontRemoveAttribute));
ThrowIfAttributePresent(typeof(ILLinkLinkAttributes), nameof(_fieldWithCustomAttribute), nameof(AllowNullAttribute));
return 100;
}
[TestDontRemoveAttribute]
[TestRemoveAttribute]
[AllowNullAttribute]
private string _fieldWithCustomAttribute = "Hello world";
class TestDontRemoveAttribute : Attribute
{
public TestDontRemoveAttribute()
{
}
}
class TestRemoveAttribute : Attribute
{
public TestRemoveAttribute()
{
}
}
class TestMarkAllRemoveAttribute : Attribute
{
}
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2070:UnrecognizedReflectionPattern",
Justification = "That's the point")]
private static bool IsTypePresent(Type testType, string typeName) => testType.GetNestedType(typeName, BindingFlags.NonPublic | BindingFlags.Public) != null;
private static void ThrowIfTypeNotPresent(Type testType, string typeName)
{
if (!IsTypePresent(testType, typeName))
{
throw new Exception(typeName);
}
}
private static void ThrowIfTypePresent(Type testType, string typeName)
{
if (IsTypePresent(testType, typeName))
{
throw new Exception(typeName);
}
}
private static bool IsAttributePresent(Type testType, string memberName, string attributeName)
{
foreach (var member in testType.GetMembers())
{
if (member.Name == memberName)
{
foreach (var attribute in member.GetCustomAttributes(false))
{
if (attribute.GetType().Name == attributeName)
{
return true;
}
}
}
}
return false;
}
private static void ThrowIfAttributeNotPresent(Type testType, string memberName, string attributeName)
{
if (!IsAttributePresent(testType, memberName, attributeName))
{
throw new Exception(attributeName);
}
}
private static void ThrowIfAttributePresent(Type testType, string memberName, string attributeName)
{
if (IsAttributePresent(testType, memberName, attributeName))
{
throw new Exception(attributeName);
}
}
}