From cc52d103157c8a861ed053d20180ca3b18352147 Mon Sep 17 00:00:00 2001 From: vitek-karas Date: Wed, 19 Feb 2020 06:03:21 -0800 Subject: [PATCH] Add PreserveDependency to workaround linker limitation Both cases have type references to a different assembly and linker currently can't add a new assembly to the closure when analyzing Type.GetType - so PreserveDepenency attribute is necessary in this case. --- .../src/System/Threading/SynchronizationContext.Uap.cs | 4 ++++ .../src/System/Resources/ResourceManager.Uap.cs | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/src/coreclr/src/System.Private.CoreLib/src/System/Threading/SynchronizationContext.Uap.cs b/src/coreclr/src/System.Private.CoreLib/src/System/Threading/SynchronizationContext.Uap.cs index 7cdf7fc..b644b28 100644 --- a/src/coreclr/src/System.Private.CoreLib/src/System/Threading/SynchronizationContext.Uap.cs +++ b/src/coreclr/src/System.Private.CoreLib/src/System/Threading/SynchronizationContext.Uap.cs @@ -48,6 +48,10 @@ namespace System.Threading private static Func? s_createSynchronizationContextDelegate; + // This is necessary because linker can't add new assemblies to the closure when recognizing Type.GetType + // so even though the GetType call below is analyzable, the PreserveDependency is still necessary to actually include + // the assembly in the trimmed closure. + [PreserveDependency(".ctor()", "System.Threading.WinRTSynchronizationContextFactory", "System.Runtime.WindowsRuntime")] private static SynchronizationContext GetWinRTSynchronizationContext(object dispatcher) { // diff --git a/src/libraries/System.Private.CoreLib/src/System/Resources/ResourceManager.Uap.cs b/src/libraries/System.Private.CoreLib/src/System/Resources/ResourceManager.Uap.cs index 27ed243..2fa9e0d 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Resources/ResourceManager.Uap.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Resources/ResourceManager.Uap.cs @@ -6,6 +6,7 @@ using System.IO; using System.Globalization; using System.Reflection; using System.Diagnostics; +using System.Runtime.CompilerServices; using Internal.Resources; namespace System.Resources @@ -57,6 +58,10 @@ namespace System.Resources // Since we can't directly reference System.Runtime.WindowsRuntime from System.Private.CoreLib, we have to get the type via reflection. // It would be better if we could just implement WindowsRuntimeResourceManager in System.Private.CoreLib, but we can't, because // we can do very little with WinRT in System.Private.CoreLib. + // The attribute is necessary because linker can't add new assemblies to the closure when recognizing Type.GetType + // so even though the GetType call below is analyzable, the PreserveDependency is still necessary to actually include + // the assembly in the trimmed closure. + [PreserveDependency(".ctor()", "System.Resources.WindowsRuntimeResourceManager", "System.Runtime.WindowsRuntime")] internal static WindowsRuntimeResourceManagerBase GetWinRTResourceManager() { Type WinRTResourceManagerType = Type.GetType("System.Resources.WindowsRuntimeResourceManager, System.Runtime.WindowsRuntime", throwOnError: true)!; -- 2.7.4