-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
ILLinkDescriptor.cs
96 lines (79 loc) · 2.95 KB
/
ILLinkDescriptor.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
// 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 ILLinkDescriptor
{
public static int Run()
{
ThrowIfMemberNotPresent(typeof(ILLinkDescriptor), nameof(methodKeptViaDescriptor));
ThrowIfMemberNotPresent(typeof(ILLinkDescriptor), nameof(methodKeptViaStandaloneDescriptor));
ThrowIfMemberPresent(typeof(ILLinkDescriptor), nameof(methodNotKept));
ThrowIfMemberNotPresent(typeof(ILLinkDescriptor), nameof(fieldKeptViaDescriptor));
ThrowIfMemberNotPresent(typeof(ILLinkDescriptor), nameof(PropertyKeptViaDescriptor));
ThrowIfMemberNotPresent(typeof(ILLinkDescriptor), nameof(EventKeptViaDescriptor));
ThrowIfTypeNotPresent(typeof(ILLinkDescriptor), nameof(NestedTypeKeptViaDescriptor));
ThrowIfTypePresent(typeof(ILLinkDescriptor), nameof(NestedTypeNonKept));
return 100;
}
public static void methodKeptViaDescriptor()
{
}
public static void methodKeptViaStandaloneDescriptor()
{
}
public static void methodNotKept()
{
}
public int fieldKeptViaDescriptor;
public int PropertyKeptViaDescriptor { get; set; }
public event EventHandler EventKeptViaDescriptor { add { } remove { } }
class NestedTypeKeptViaDescriptor
{
}
class NestedTypeNonKept
{
}
[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;
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2070:UnrecognizedReflectionPattern",
Justification = "That's the point")]
private static bool IsMemberPresent(Type testType, string memberName) {
foreach (var member in testType.GetMembers())
{
if (member.Name == memberName)
return true;
}
return false;
}
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 void ThrowIfMemberNotPresent(Type testType, string memberName)
{
if (!IsMemberPresent(testType, memberName))
{
throw new Exception(memberName);
}
}
private static void ThrowIfMemberPresent(Type testType, string memberName)
{
if (IsMemberPresent(testType, memberName))
{
throw new Exception(memberName);
}
}
}