|
| 1 | +// Copyright (c) .NET Foundation and contributors. All rights reserved. |
| 2 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Diagnostics.CodeAnalysis; |
| 7 | +using System.Runtime.CompilerServices; |
| 8 | +using System.Runtime.InteropServices; |
| 9 | +using Mono.Linker.Tests.Cases.Expectations.Assertions; |
| 10 | +using Mono.Linker.Tests.Cases.Reflection; |
| 11 | + |
| 12 | +[assembly: TypeMap<UsedTypeMap> ("TrimTargetIsTarget", typeof (TargetAndTrimTarget), typeof (TargetAndTrimTarget))] |
| 13 | +[assembly: TypeMap<UsedTypeMap> ("TrimTargetIsUnrelated", typeof (TargetType), typeof (TrimTarget))] |
| 14 | +[assembly: TypeMap<UsedTypeMap> ("TrimTargetIsUnreferenced", typeof (UnreferencedTargetType), typeof (UnreferencedTrimTarget))] |
| 15 | +[assembly: TypeMapAssociation<UsedTypeMap> (typeof (SourceClass), typeof (ProxyType))] |
| 16 | + |
| 17 | +[assembly: TypeMap<UnusedTypeMap> ("UnusedName", typeof (UnusedTargetType), typeof (TrimTarget))] |
| 18 | +[assembly: TypeMapAssociation<UsedTypeMap> (typeof (UnusedSourceClass), typeof (UnusedProxyType))] |
| 19 | + |
| 20 | +namespace Mono.Linker.Tests.Cases.Reflection |
| 21 | +{ |
| 22 | + [Kept] |
| 23 | + [IgnoreTestCase("Trimmer support is currently not implemented", IgnoredBy = Tool.Trimmer)] |
| 24 | + class TypeMap |
| 25 | + { |
| 26 | + [Kept] |
| 27 | + public static void Main(string[] args) |
| 28 | + { |
| 29 | + object t = Activator.CreateInstance (Type.GetType (args[1])); |
| 30 | + if (t is TargetAndTrimTarget) { |
| 31 | + Console.WriteLine ("Type deriving from TargetAndTrimTarget instantiated."); |
| 32 | + } else if (t is TrimTarget) { |
| 33 | + Console.WriteLine ("Type deriving from TrimTarget instantiated."); |
| 34 | + } |
| 35 | + |
| 36 | + Console.WriteLine ("Hash code of SourceClass instance: " + new SourceClass ().GetHashCode ()); |
| 37 | + |
| 38 | + Console.WriteLine (TypeMapping.GetOrCreateExternalTypeMapping<UsedTypeMap> ()); |
| 39 | + Console.WriteLine (TypeMapping.GetOrCreateProxyTypeMapping<UsedTypeMap> ()); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + [Kept(By = Tool.Trimmer)] |
| 44 | + class UsedTypeMap; |
| 45 | + |
| 46 | + [Kept] |
| 47 | + class TargetAndTrimTarget; |
| 48 | + |
| 49 | + [Kept] |
| 50 | + class TargetType; |
| 51 | + |
| 52 | + [Kept] |
| 53 | + class TrimTarget; |
| 54 | + |
| 55 | + class UnreferencedTargetType; |
| 56 | + |
| 57 | + class UnreferencedTrimTarget; |
| 58 | + |
| 59 | + [Kept] |
| 60 | + [KeptMember(".ctor()")] |
| 61 | + class SourceClass; |
| 62 | + |
| 63 | + [Kept] |
| 64 | + class ProxyType; |
| 65 | + |
| 66 | + class UnusedTypeMap; |
| 67 | + class UnusedTargetType; |
| 68 | + class UnusedSourceClass; |
| 69 | + class UnusedProxyType; |
| 70 | +} |
| 71 | + |
| 72 | +// Polyfill for the type map types until we use an LKG runtime that has it. |
| 73 | +namespace System.Runtime.InteropServices |
| 74 | +{ |
| 75 | + [Kept (By = Tool.Trimmer)] |
| 76 | + [KeptBaseType (typeof (Attribute), By = Tool.Trimmer)] |
| 77 | + [KeptAttributeAttribute (typeof (AttributeUsageAttribute), By = Tool.Trimmer)] |
| 78 | + [AttributeUsage (AttributeTargets.Assembly, AllowMultiple = true)] |
| 79 | + public sealed class TypeMapAttribute<TTypeMapGroup> : Attribute |
| 80 | + { |
| 81 | + [Kept (By = Tool.Trimmer)] |
| 82 | + public TypeMapAttribute (string value, Type target) { } |
| 83 | + |
| 84 | + [Kept (By = Tool.Trimmer)] |
| 85 | + [KeptAttributeAttribute (typeof (RequiresUnreferencedCodeAttribute), By = Tool.Trimmer)] |
| 86 | + [RequiresUnreferencedCode ("Interop types may be removed by trimming")] |
| 87 | + public TypeMapAttribute (string value, Type target, Type trimTarget) { } |
| 88 | + } |
| 89 | + |
| 90 | + [Kept (By = Tool.Trimmer)] |
| 91 | + [KeptBaseType (typeof (Attribute), By = Tool.Trimmer)] |
| 92 | + [KeptAttributeAttribute (typeof (AttributeUsageAttribute), By = Tool.Trimmer)] |
| 93 | + [AttributeUsage (AttributeTargets.Assembly, AllowMultiple = true)] |
| 94 | + public sealed class TypeMapAssociationAttribute<TTypeMapGroup> : Attribute |
| 95 | + { |
| 96 | + [Kept (By = Tool.Trimmer)] |
| 97 | + public TypeMapAssociationAttribute (Type source, Type proxy) { } |
| 98 | + } |
| 99 | + |
| 100 | + [Kept(By = Tool.Trimmer)] |
| 101 | + public static class TypeMapping |
| 102 | + { |
| 103 | + [Kept(By = Tool.Trimmer)] |
| 104 | + [KeptAttributeAttribute (typeof (RequiresUnreferencedCodeAttribute), By = Tool.Trimmer)] |
| 105 | + [RequiresUnreferencedCode ("Interop types may be removed by trimming")] |
| 106 | + public static IReadOnlyDictionary<string, Type> GetOrCreateExternalTypeMapping<TTypeMapGroup> () |
| 107 | + { |
| 108 | + throw new NotImplementedException (); |
| 109 | + } |
| 110 | + |
| 111 | + [Kept(By = Tool.Trimmer)] |
| 112 | + [KeptAttributeAttribute (typeof (RequiresUnreferencedCodeAttribute), By = Tool.Trimmer)] |
| 113 | + [RequiresUnreferencedCode ("Interop types may be removed by trimming")] |
| 114 | + public static IReadOnlyDictionary<Type, Type> GetOrCreateProxyTypeMapping<TTypeMapGroup> () |
| 115 | + { |
| 116 | + throw new NotImplementedException (); |
| 117 | + } |
| 118 | + } |
| 119 | +} |
| 120 | + |
0 commit comments