コード例 #1
0
        /// <summary>Tries to get the value of a switch. </summary>
        /// <param name="switchName">The name of the switch. </param>
        /// <param name="isEnabled">When this method returns, contains the value of <paramref name="switchName" /> if <paramref name="switchName" /> was found, or <see langword="false" /> if <paramref name="switchName" /> was not found. This parameter is passed uninitialized. </param>
        /// <returns>
        ///     <see langword="true" /> if <paramref name="switchName" /> was set and the <paramref name="isEnabled" /> argument contains the value of the switch; otherwise, <see langword="false" />. </returns>
        /// <exception cref="T:System.ArgumentNullException">
        ///         <paramref name="switchName" /> is <see langword="null" />. </exception>
        /// <exception cref="T:System.ArgumentException">
        ///         <paramref name="switchName" /> is <see cref="F:System.String.Empty" />. </exception>
        // Token: 0x06000205 RID: 517 RVA: 0x00005304 File Offset: 0x00003504
        public static bool TryGetSwitch(string switchName, out bool isEnabled)
        {
            if (switchName == null)
            {
                throw new ArgumentNullException("switchName");
            }
            if (switchName.Length == 0)
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_EmptyName"), "switchName");
            }
            if (!AppContext.s_defaultsInitialized)
            {
                AppContext.InitializeDefaultSwitchValues();
            }
            isEnabled = false;
            Dictionary <string, AppContext.SwitchValueState> obj = AppContext.s_switchMap;

            lock (obj)
            {
                AppContext.SwitchValueState switchValueState;
                if (AppContext.s_switchMap.TryGetValue(switchName, out switchValueState))
                {
                    if (switchValueState == AppContext.SwitchValueState.UnknownValue)
                    {
                        isEnabled = false;
                        return(false);
                    }
                    isEnabled = ((switchValueState & AppContext.SwitchValueState.HasTrueValue) == AppContext.SwitchValueState.HasTrueValue);
                    if ((switchValueState & AppContext.SwitchValueState.HasLookedForOverride) == AppContext.SwitchValueState.HasLookedForOverride)
                    {
                        return(true);
                    }
                    bool flag2;
                    if (AppContextDefaultValues.TryGetSwitchOverride(switchName, out flag2))
                    {
                        isEnabled = flag2;
                    }
                    AppContext.s_switchMap[switchName] = ((isEnabled ? AppContext.SwitchValueState.HasTrueValue : AppContext.SwitchValueState.HasFalseValue) | AppContext.SwitchValueState.HasLookedForOverride);
                    return(true);
                }
                else
                {
                    bool flag3;
                    if (AppContextDefaultValues.TryGetSwitchOverride(switchName, out flag3))
                    {
                        isEnabled = flag3;
                        AppContext.s_switchMap[switchName] = ((isEnabled ? AppContext.SwitchValueState.HasTrueValue : AppContext.SwitchValueState.HasFalseValue) | AppContext.SwitchValueState.HasLookedForOverride);
                        return(true);
                    }
                    AppContext.s_switchMap[switchName] = AppContext.SwitchValueState.UnknownValue;
                }
            }
            return(false);
        }
コード例 #2
0
 /// <summary>Trues 来获取一个开关的值。</summary>
 /// <returns>true如果<paramref name="switchName" />设置和<paramref name="isEnabled" />参数中包含的开关 ; 值否则为false。</returns>
 /// <param name="switchName">开关的名称。</param>
 /// <param name="isEnabled">此方法返回时,包含值的<paramref name="switchName" />如果<paramref name="switchName" />找,或false如果<paramref name="switchName" />找不到。此参数未经初始化即被传递。</param>
 /// <exception cref="T:System.ArgumentNullException">
 /// <paramref name="switchName" /> 为 null。</exception>
 /// <exception cref="T:System.ArgumentException">
 /// <paramref name="switchName" /> 为 <see cref="F:System.String.Empty" />。</exception>
 public static bool TryGetSwitch(string switchName, out bool isEnabled)
 {
     if (switchName == null)
     {
         throw new ArgumentNullException("switchName");
     }
     if (switchName.Length == 0)
     {
         throw new ArgumentException(Environment.GetResourceString("Argument_EmptyName"), "switchName");
     }
     isEnabled = false;
     lock (AppContext.s_switchMap)
     {
         AppContext.SwitchValueState local_0;
         if (AppContext.s_switchMap.TryGetValue(switchName, out local_0))
         {
             if (local_0 == AppContext.SwitchValueState.UnknownValue)
             {
                 isEnabled = false;
                 return(false);
             }
             isEnabled = (local_0 & AppContext.SwitchValueState.HasTrueValue) == AppContext.SwitchValueState.HasTrueValue;
             if ((local_0 & AppContext.SwitchValueState.HasLookedForOverride) == AppContext.SwitchValueState.HasLookedForOverride)
             {
                 return(true);
             }
             bool local_3;
             if (AppContextDefaultValues.TryGetSwitchOverride(switchName, out local_3))
             {
                 isEnabled = local_3;
             }
             AppContext.s_switchMap[switchName] = (AppContext.SwitchValueState)((isEnabled ? 2 : 1) | 4);
             return(true);
         }
         bool local_5;
         if (AppContextDefaultValues.TryGetSwitchOverride(switchName, out local_5))
         {
             isEnabled = local_5;
             AppContext.s_switchMap[switchName] = (AppContext.SwitchValueState)((isEnabled ? 2 : 1) | 4);
             return(true);
         }
         AppContext.s_switchMap[switchName] = AppContext.SwitchValueState.UnknownValue;
     }
     return(false);
 }
コード例 #3
0
ファイル: AppContext.cs プロジェクト: xied75/coreclr
        /// <summary>
        /// Try to get the value of the switch.
        /// </summary>
        /// <param name="switchName">The name of the switch</param>
        /// <param name="isEnabled">A variable where to place the value of the switch</param>
        /// <returns>A return value of true represents that the switch was set and <paramref name="isEnabled"/> contains the value of the switch</returns>
        public static bool TryGetSwitch(string switchName, out bool isEnabled)
        {
            if (switchName == null)
            {
                throw new ArgumentNullException(nameof(switchName));
            }
            if (switchName.Length == 0)
            {
                throw new ArgumentException(SR.Argument_EmptyName, nameof(switchName));
            }

            // By default, the switch is not enabled.
            isEnabled = false;

            SwitchValueState switchValue;

            lock (s_switchMap)
            {
                if (s_switchMap.TryGetValue(switchName, out switchValue))
                {
                    // The value is in the dictionary.
                    // There are 3 cases here:
                    // 1. The value of the switch is 'unknown'. This means that the switch name is not known to the system (either via defaults or checking overrides).
                    //    Example: This is the case when, during a servicing event, a switch is added to System.Xml which ships before mscorlib. The value of the switch
                    //             Will be unknown to mscorlib.dll and we want to prevent checking the overrides every time we check this switch
                    // 2. The switch has a valid value AND we have read the overrides for it
                    //    Example: TryGetSwitch is called for a switch set via SetSwitch
                    // 3. The switch has the default value and we need to check for overrides
                    //    Example: TryGetSwitch is called for the first time for a switch that has a default value

                    // 1. The value is unknown
                    if (switchValue == SwitchValueState.UnknownValue)
                    {
                        isEnabled = false;
                        return(false);
                    }

                    // We get the value of isEnabled from the value that we stored in the dictionary
                    isEnabled = (switchValue & SwitchValueState.HasTrueValue) == SwitchValueState.HasTrueValue;

                    // 2. The switch has a valid value AND we have checked for overrides
                    if ((switchValue & SwitchValueState.HasLookedForOverride) == SwitchValueState.HasLookedForOverride)
                    {
                        return(true);
                    }

                    // 3. The switch has a valid value, but we need to check for overrides.
                    // Regardless of whether or not the switch has an override, we need to update the value to reflect
                    // the fact that we checked for overrides.
                    bool overrideValue;
                    if (AppContextDefaultValues.TryGetSwitchOverride(switchName, out overrideValue))
                    {
                        // we found an override!
                        isEnabled = overrideValue;
                    }

                    // Update the switch in the dictionary to mark it as 'checked for override'
                    s_switchMap[switchName] = (isEnabled ? SwitchValueState.HasTrueValue : SwitchValueState.HasFalseValue)
                                              | SwitchValueState.HasLookedForOverride;

                    return(true);
                }
                else
                {
                    // The value is NOT in the dictionary
                    // In this case we need to see if we have an override defined for the value.
                    // There are 2 cases:
                    // 1. The value has an override specified. In this case we need to add the value to the dictionary
                    //    and mark it as checked for overrides
                    //    Example: In a servicing event, System.Xml introduces a switch and an override is specified.
                    //             The value is not found in mscorlib (as System.Xml ships independent of mscorlib)
                    // 2. The value does not have an override specified
                    //    In this case, we want to capture the fact that we looked for a value and found nothing by adding
                    //    an entry in the dictionary with the 'sentinel' value of 'SwitchValueState.UnknownValue'.
                    //    Example: This will prevent us from trying to find overrides for values that we don't have in the dictionary

                    // 1. The value has an override specified.
                    bool overrideValue;
                    if (AppContextDefaultValues.TryGetSwitchOverride(switchName, out overrideValue))
                    {
                        isEnabled = overrideValue;

                        // Update the switch in the dictionary to mark it as 'checked for override'
                        s_switchMap[switchName] = (isEnabled ? SwitchValueState.HasTrueValue : SwitchValueState.HasFalseValue)
                                                  | SwitchValueState.HasLookedForOverride;

                        return(true);
                    }

                    // 2. The value does not have an override.
                    s_switchMap[switchName] = SwitchValueState.UnknownValue;
                }
            }
            return(false); // we did not find a value for the switch
        }