// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using System; using System.Runtime.CompilerServices; namespace CommunityToolkit.Mvvm; /// /// A container for all shared configuration switches for the MVVM Toolkit. /// /// /// /// This type uses a very specific setup for configuration switches to ensure ILLink can work the best. /// This mirrors the architecture of feature switches in the runtime as well, and it's needed so that /// no static constructor is generated for the type. /// /// /// For more info, see . /// /// internal static class FeatureSwitches { /// /// The configuration property name for . /// private const string EnableINotifyPropertyChangingSupportPropertyName = "MVVMTOOLKIT_ENABLE_INOTIFYPROPERTYCHANGING_SUPPORT"; /// /// The backing field for . /// private static int enableINotifyPropertyChangingSupport; /// /// Gets a value indicating whether or not support for should be enabled (defaults to ). /// public static bool EnableINotifyPropertyChangingSupport { [MethodImpl(MethodImplOptions.AggressiveInlining)] get => GetConfigurationValue(EnableINotifyPropertyChangingSupportPropertyName, ref enableINotifyPropertyChangingSupport, true); } /// /// Gets a configuration value for a specified property. /// /// The property name to retrieve the value for. /// The cached result for the target configuration value. /// The default value for the feature switch, if not set. /// The value of the specified configuration setting. private static bool GetConfigurationValue(string propertyName, ref int cachedResult, bool defaultValue) { // The cached switch value has 3 states: // 0: unknown. // 1: true // -1: false // // This method doesn't need to worry about concurrent accesses to the cached result, // as even if the configuration value is retrieved twice, that'll always be the same. if (cachedResult < 0) { return false; } if (cachedResult > 0) { return true; } // Get the configuration switch value, or its default. // All feature switches have a default set in the .targets file. if (!AppContext.TryGetSwitch(propertyName, out bool isEnabled)) { isEnabled = defaultValue; } // Update the cached result cachedResult = isEnabled ? 1 : -1; return isEnabled; } }