Fix trim warning in ComponentActivator (#82952)
authorElinor Fung <elfung@microsoft.com>
Fri, 3 Mar 2023 21:28:54 +0000 (13:28 -0800)
committerGitHub <noreply@github.com>
Fri, 3 Mar 2023 21:28:54 +0000 (15:28 -0600)
* Fix trim warning in ComponentActivator

* Add UnsupportedOSPlatform attributes

src/libraries/System.Private.CoreLib/src/Internal/Runtime/InteropServices/ComponentActivator.cs

index c40d18e..402d1a5 100644 (file)
@@ -140,26 +140,34 @@ namespace Internal.Runtime.InteropServices
 
             [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
                 Justification = "The same feature switch applies to GetFunctionPointer and this function. We rely on the warning from GetFunctionPointer.")]
-            static void LoadAssemblyLocal(string assemblyPath)
+            static void LoadAssemblyLocal(string assemblyPath) => LoadAssemblyImpl(assemblyPath);
+        }
+
+        [RequiresUnreferencedCode(TrimIncompatibleWarningMessage, Url = "https://aka.ms/dotnet-illink/nativehost")]
+        [UnsupportedOSPlatform("android")]
+        [UnsupportedOSPlatform("browser")]
+        [UnsupportedOSPlatform("ios")]
+        [UnsupportedOSPlatform("maccatalyst")]
+        [UnsupportedOSPlatform("tvos")]
+        private static void LoadAssemblyImpl(string assemblyPath)
+        {
+            lock(s_loadedInDefaultContext)
             {
-                lock(s_loadedInDefaultContext)
-                {
-                    if (s_loadedInDefaultContext.Contains(assemblyPath))
-                        return;
-
-                    var resolver = new AssemblyDependencyResolver(assemblyPath);
-                    AssemblyLoadContext.Default.Resolving +=
-                        (context, assemblyName) =>
-                        {
-                            string? assemblyPath = resolver.ResolveAssemblyToPath(assemblyName);
-                            return assemblyPath != null
-                                ? context.LoadFromAssemblyPath(assemblyPath)
-                                : null;
-                        };
-
-                    AssemblyLoadContext.Default.LoadFromAssemblyPath(assemblyPath);
-                    s_loadedInDefaultContext.Add(assemblyPath);
-                }
+                if (s_loadedInDefaultContext.Contains(assemblyPath))
+                    return;
+
+                var resolver = new AssemblyDependencyResolver(assemblyPath);
+                AssemblyLoadContext.Default.Resolving +=
+                    (context, assemblyName) =>
+                    {
+                        string? assemblyPath = resolver.ResolveAssemblyToPath(assemblyName);
+                        return assemblyPath != null
+                            ? context.LoadFromAssemblyPath(assemblyPath)
+                            : null;
+                    };
+
+                AssemblyLoadContext.Default.LoadFromAssemblyPath(assemblyPath);
+                s_loadedInDefaultContext.Add(assemblyPath);
             }
         }