|
5 | 5 | using System.Collections.Generic; |
6 | 6 | using System.Diagnostics.CodeAnalysis; |
7 | 7 | using System.Linq; |
| 8 | +using System.Reflection; |
8 | 9 | using System.Text; |
9 | 10 | using System.Threading.Tasks; |
10 | 11 | using Mono.Linker.Tests.Cases.Expectations.Assertions; |
@@ -50,6 +51,8 @@ public static void Main () |
50 | 51 | TestNoValue (); |
51 | 52 |
|
52 | 53 | Mixed_Derived.Test (typeof (TestType), 0); |
| 54 | + |
| 55 | + LoopPatterns.Test (); |
53 | 56 | } |
54 | 57 |
|
55 | 58 | static void TestAllPropagated ([DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.All)] Type derivedType) |
@@ -275,6 +278,62 @@ public static void Test ( |
275 | 278 | } |
276 | 279 | } |
277 | 280 |
|
| 281 | + class LoopPatterns |
| 282 | + { |
| 283 | + static void EnumerateInterfacesOnBaseTypes ([DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.Interfaces)] Type type) |
| 284 | + { |
| 285 | + Type? t = type; |
| 286 | + while (t != null) { |
| 287 | + Type[] interfaces = t.GetInterfaces (); |
| 288 | + t = t.BaseType; |
| 289 | + } |
| 290 | + } |
| 291 | + |
| 292 | + [ExpectedWarning ("IL2070")] |
| 293 | + [ExpectedWarning ("IL2075", ProducedBy = ProducedBy.Analyzer)] // Linker doesn't implement backward branches data flow yet |
| 294 | + static void EnumerateInterfacesOnBaseTypes_Unannotated (Type type) |
| 295 | + { |
| 296 | + Type? t = type; |
| 297 | + while (t != null) { |
| 298 | + Type[] interfaces = t.GetInterfaces (); |
| 299 | + t = t.BaseType; |
| 300 | + } |
| 301 | + } |
| 302 | + |
| 303 | + // Can only work with All annotation as NonPublicProperties doesn't propagate to base types |
| 304 | + static void EnumeratePrivatePropertiesOnBaseTypes ([DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.All)] Type type) |
| 305 | + { |
| 306 | + const BindingFlags DeclaredOnlyLookup = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly; |
| 307 | + Type? t = type; |
| 308 | + while (t != null) { |
| 309 | + t.GetProperties (DeclaredOnlyLookup).GetEnumerator (); |
| 310 | + t = t.BaseType; |
| 311 | + } |
| 312 | + } |
| 313 | + |
| 314 | + // Can only work with All annotation as NonPublicProperties doesn't propagate to base types |
| 315 | + static void EnumeratePrivatePropertiesOnBaseTypesWithForeach ([DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.All)] Type type) |
| 316 | + { |
| 317 | + const BindingFlags DeclaredOnlyLookup = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly; |
| 318 | + Type? t = type; |
| 319 | + while (t != null) { |
| 320 | + foreach (var p in t.GetProperties (DeclaredOnlyLookup)) { |
| 321 | + // Do nothing |
| 322 | + } |
| 323 | + t = t.BaseType; |
| 324 | + } |
| 325 | + } |
| 326 | + |
| 327 | + public static void Test () |
| 328 | + { |
| 329 | + EnumerateInterfacesOnBaseTypes (typeof (TestType)); |
| 330 | + EnumerateInterfacesOnBaseTypes_Unannotated (typeof (TestType)); |
| 331 | + |
| 332 | + EnumeratePrivatePropertiesOnBaseTypes (typeof (TestType)); |
| 333 | + EnumeratePrivatePropertiesOnBaseTypesWithForeach (typeof (TestType)); |
| 334 | + } |
| 335 | + } |
| 336 | + |
278 | 337 | class TestType |
279 | 338 | { |
280 | 339 | } |
|
0 commit comments