private static void PopulateOverrideValuesPartial()
        {
            string appContextOverridesInternalCall = CompatibilitySwitch.GetAppContextOverridesInternalCall();

            if (string.IsNullOrEmpty(appContextOverridesInternalCall))
            {
                return;
            }
            bool flag  = false;
            bool flag2 = false;
            bool flag3 = false;
            int  num   = -1;
            int  num2  = -1;

            for (int i = 0; i <= appContextOverridesInternalCall.Length; i++)
            {
                if (i == appContextOverridesInternalCall.Length || appContextOverridesInternalCall[i] == ';')
                {
                    if (flag && flag2 && flag3)
                    {
                        int    startIndex  = num + 1;
                        int    length      = num2 - num - 1;
                        string switchName  = appContextOverridesInternalCall.Substring(startIndex, length);
                        int    startIndex2 = num2 + 1;
                        int    length2     = i - num2 - 1;
                        string value       = appContextOverridesInternalCall.Substring(startIndex2, length2);
                        bool   isEnabled;
                        if (bool.TryParse(value, out isEnabled))
                        {
                            AppContext.DefineSwitchOverride(switchName, isEnabled);
                        }
                    }
                    num   = i;
                    flag3 = (flag2 = (flag = false));
                }
                else if (appContextOverridesInternalCall[i] == '=')
                {
                    if (!flag)
                    {
                        flag = true;
                        num2 = i;
                    }
                }
                else if (flag)
                {
                    flag3 = true;
                }
                else
                {
                    flag2 = true;
                }
            }
        }
Beispiel #2
0
        static partial void PopulateOverrideValuesPartial()
        {
            // Retrieve the value from EE config.
            string overrides = System.Runtime.Versioning.CompatibilitySwitch.GetAppContextOverridesInternalCall();

            // If we have no override values, do nothing.
            if (string.IsNullOrEmpty(overrides))
            {
                return;
            }

            bool encounteredEquals = false, encounteredCharsInKey = false, encounteredCharsInValue = false;
            int  previousSemicolonPos = -1, firstEqualsPos = -1;

            // Iterate over the string one character at a time until we reach the end of the string.
            for (int currentPos = 0; currentPos <= overrides.Length; currentPos++)
            {
                // If the current position is either ';' or 'end-of-string' then we potentially have a key=value pair
                if (currentPos == overrides.Length || overrides[currentPos] == ';')
                {
                    // We only have a key=value pair if we encountered an equals, characters in the key and in the value
                    // portion of the pair.
                    if (encounteredEquals && encounteredCharsInKey && encounteredCharsInValue)
                    {
                        // We compute the indexes in the string for key and value
                        int    firstCharOfKey = previousSemicolonPos + 1;                  //+1 because we don't take the ';' char
                        int    lenghtOfKey    = firstEqualsPos - previousSemicolonPos - 1; //-1 because we don't take the '=' char
                        string name           = overrides.Substring(firstCharOfKey, lenghtOfKey);

                        int    firstCharOfValue = firstEqualsPos + 1;              // +1 because we don't count the '='
                        int    lengthOfValue    = currentPos - firstEqualsPos - 1; // -1 because we don't count the '='
                        string value            = overrides.Substring(firstCharOfValue, lengthOfValue);

                        // apply the value only if it parses as a boolean
                        bool switchValue;
                        if (bool.TryParse(value, out switchValue))
                        {
                            // If multiple switches have the same name, the last value that we find will win.
                            AppContext.DefineSwitchOverride(name, switchValue);
                        }
                    }
                    previousSemicolonPos = currentPos;

                    // We need to reset these flags once we encounter a ';'
                    encounteredCharsInKey = encounteredCharsInValue = encounteredEquals = false;
                }
                else if (overrides[currentPos] == '=')
                {
                    // if the current character is '=' then we should flag it and remember it
                    if (!encounteredEquals)
                    {
                        encounteredEquals = true;
                        firstEqualsPos    = currentPos;
                    }
                }
                else
                {
                    // We need to know if the key or value contain any characters (other than ';' and '=');
                    if (encounteredEquals)
                    {
                        encounteredCharsInValue = true;
                    }
                    else
                    {
                        encounteredCharsInKey = true;
                    }
                }
            }
        }