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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
// 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.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Mono.Linker.Tests.Cases.Expectations.Assertions;
using Mono.Linker.Tests.Cases.Expectations.Helpers;
namespace Mono.Linker.Tests.Cases.DataFlow
{
[SkipKeptItemsValidation]
[ExpectedNoWarnings]
public class GetInterfaceDataFlow
{
public static void Main ()
{
GetInterface_Name.Test ();
GetInterface_Name_IgnoreCase.Test ();
}
class GetInterface_Name
{
static void TestNullName (Type type)
{
type.GetInterface (null);
}
static void TestEmptyName (Type type)
{
type.GetInterface (string.Empty);
}
static void TestNoValueName (Type type)
{
Type t = null;
string noValue = t.AssemblyQualifiedName;
type.GetInterface (noValue);
}
[ExpectedWarning ("IL2070", nameof (Type.GetInterface), nameof (DynamicallyAccessedMemberTypes) + "." + nameof (DynamicallyAccessedMemberTypes.Interfaces))]
static void TestNoAnnotation (Type type)
{
type.GetInterface ("ITestInterface");
}
[ExpectedWarning ("IL2072", nameof (DataFlowTypeExtensions.RequiresAll), nameof (DynamicallyAccessedMemberTypes) + "." + nameof (DynamicallyAccessedMemberTypes.All))]
static void TestWithAnnotation ([DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.Interfaces)] Type type)
{
type.GetInterface ("ITestInterface").RequiresInterfaces ();
type.GetInterface ("ITestInterface").RequiresAll (); // Warns
}
static void TestWithAll ([DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.All)] Type type)
{
type.GetInterface ("ITestInterface").RequiresInterfaces ();
type.GetInterface ("ITestInterface").RequiresAll ();
}
[ExpectedWarning ("IL2072", nameof (DataFlowTypeExtensions.RequiresAll), nameof (DynamicallyAccessedMemberTypes) + "." + nameof (DynamicallyAccessedMemberTypes.All))]
static void TestKnownType ()
{
// Interfaces marking is transitive - meaning that it marks all interfaces in the entire hierarchy
// so the return value of GetInterface always has all interfaces on it already.
typeof (TestType).GetInterface ("ITestInterface").RequiresInterfaces ();
typeof (TestType).GetInterface ("ITestInterface").RequiresAll (); // Warns
}
[ExpectedWarning ("IL2072", nameof (DataFlowTypeExtensions.RequiresAll), nameof (DynamicallyAccessedMemberTypes) + "." + nameof (DynamicallyAccessedMemberTypes.All))]
static void TestMultipleValues (int p, [DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.All)] Type typeWithAll)
{
Type type;
if (p == 0)
type = typeof (TestType);
else
type = typeWithAll;
type.GetInterface ("ITestInterface").RequiresInterfaces ();
type.GetInterface ("ITestInterface").RequiresAll (); // Warns since only one of the values is guaranteed All
}
[ExpectedWarning ("IL2075", nameof (DynamicallyAccessedMemberTypes) + "." + nameof (DynamicallyAccessedMemberTypes.Interfaces))]
static void TestMergedValues (int p, [DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.All)] Type typeWithAll)
{
Type type = new TestType ().GetType ();
if (p == 0) {
type = typeWithAll;
}
type.GetInterface ("ITestInterface").RequiresInterfaces ();
}
static void TestNullValue ()
{
Type t = null;
t.GetInterface ("ITestInterface").RequiresInterfaces ();
}
static void TestNoValue ()
{
Type t = null;
Type noValue = Type.GetTypeFromHandle (t.TypeHandle);
// t.TypeHandle throws at runtime so don't warn here.
noValue.GetInterface ("ITestInterface").RequiresInterfaces ();
}
class GetInterfaceInCtor
{
public GetInterfaceInCtor ([DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.Interfaces)] Type type)
{
type.GetInterface ("ITestInterface").RequiresInterfaces ();
}
}
public static void Test ()
{
TestNullName (typeof (TestType));
TestEmptyName (typeof (TestType));
TestNoValueName (typeof (TestType));
TestNoAnnotation (typeof (TestType));
TestWithAnnotation (typeof (TestType));
TestWithAnnotation (typeof (ITestInterface));
TestWithAll (typeof (TestType));
TestKnownType ();
TestMultipleValues (0, typeof (TestType));
TestMergedValues (0, typeof (TestType));
TestNullValue ();
TestNoValue ();
var _ = new GetInterfaceInCtor (typeof (TestType));
}
}
class GetInterface_Name_IgnoreCase
{
[ExpectedWarning ("IL2070", nameof (Type.GetInterface), nameof (DynamicallyAccessedMemberTypes) + "." + nameof (DynamicallyAccessedMemberTypes.Interfaces))]
static void TestNoAnnotation (Type type)
{
type.GetInterface ("ITestInterface", false);
}
[ExpectedWarning ("IL2072", nameof (DataFlowTypeExtensions.RequiresAll), nameof (DynamicallyAccessedMemberTypes) + "." + nameof (DynamicallyAccessedMemberTypes.All))]
static void TestWithAnnotation ([DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.Interfaces)] Type type)
{
type.GetInterface ("ITestInterface", false).RequiresInterfaces ();
type.GetInterface ("ITestInterface", false).RequiresAll (); // Warns
}
static void TestWithAll ([DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.All)] Type type)
{
type.GetInterface ("ITestInterface", false).RequiresInterfaces ();
type.GetInterface ("ITestInterface", false).RequiresAll ();
}
[ExpectedWarning ("IL2072", nameof (DataFlowTypeExtensions.RequiresAll), nameof (DynamicallyAccessedMemberTypes) + "." + nameof (DynamicallyAccessedMemberTypes.All))]
static void TestKnownType ()
{
// Interfaces marking is transitive - meaning that it marks all interfaces in the entire hierarchy
// so the return value of GetInterface always has all interfaces on it already.
typeof (TestType).GetInterface ("ITestInterface", false).RequiresInterfaces ();
typeof (TestType).GetInterface ("ITestInterface", false).RequiresAll ();
}
[ExpectedWarning ("IL2072", nameof (DataFlowTypeExtensions.RequiresAll), nameof (DynamicallyAccessedMemberTypes) + "." + nameof (DynamicallyAccessedMemberTypes.All))]
static void TestMultipleValues (int p, [DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.All)] Type typeWithAll)
{
Type type;
if (p == 0)
type = typeof (TestType);
else
type = typeWithAll;
type.GetInterface ("ITestInterface", false).RequiresInterfaces ();
type.GetInterface ("ITestInterface", false).RequiresAll (); // Warns since only one of the values is guaranteed All
}
public static void Test ()
{
TestNoAnnotation (typeof (TestType));
TestWithAnnotation (typeof (TestType));
TestWithAnnotation (typeof (ITestInterface));
TestWithAll (typeof (TestType));
TestKnownType ();
TestMultipleValues (0, typeof (TestType));
}
}
interface ITestInterface
{
}
class TestType : ITestInterface
{
}
}
}
|