diff --git a/src/WinRT.Runtime/ComWrappersSupport.net5.cs b/src/WinRT.Runtime/ComWrappersSupport.net5.cs index db2eb41e0..765ad3f9d 100644 --- a/src/WinRT.Runtime/ComWrappersSupport.net5.cs +++ b/src/WinRT.Runtime/ComWrappersSupport.net5.cs @@ -1,12 +1,9 @@ using System; using System.Collections; -using System.Collections.Concurrent; -using System.Collections.Generic; using System.Linq.Expressions; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -using System.Text; using System.Threading; using WinRT.Interop; using static System.Runtime.InteropServices.ComWrappers; @@ -15,6 +12,20 @@ namespace WinRT { public static partial class ComWrappersSupport { + // Instance field and property for Singleton pattern: ComWrappers `set` method should be idempotent + private static DefaultComWrappers _instance; + private static DefaultComWrappers DefaultComWrappersInstance + { + get + { + if (_instance == null) + { + _instance = new DefaultComWrappers(); + } + return _instance; + } + } + internal static readonly ConditionalWeakTable InspectableInfoTable = new ConditionalWeakTable(); internal static readonly ThreadLocal CreateRCWType = new ThreadLocal(); @@ -30,8 +41,9 @@ private static ComWrappers ComWrappers { if (_comWrappers is null) { - _comWrappers = new DefaultComWrappers(); - ComWrappers.RegisterForTrackerSupport(_comWrappers); + var comWrappersToSet = DefaultComWrappersInstance; + ComWrappers.RegisterForTrackerSupport(comWrappersToSet); + _comWrappers = comWrappersToSet; } } } @@ -41,8 +53,13 @@ private static ComWrappers ComWrappers { lock (_comWrappersLock) { - _comWrappers = value; - ComWrappers.RegisterForTrackerSupport(_comWrappers); + if (value == null && _comWrappers == DefaultComWrappersInstance) + { + return; + } + var comWrappersToSet = value ?? DefaultComWrappersInstance; + ComWrappers.RegisterForTrackerSupport(comWrappersToSet); + _comWrappers = comWrappersToSet; } } } @@ -152,7 +169,7 @@ private static T FindDelegate(IntPtr thisPtr) /// public static void InitializeComWrappers(ComWrappers wrappers = null) { - ComWrappers = wrappers ?? new DefaultComWrappers(); + ComWrappers = wrappers; } internal static Func GetTypedRcwFactory(string runtimeClassName) => TypedObjectFactoryCache.GetOrAdd(runtimeClassName, className => CreateTypedRcwFactory(className));