public static bool TryGetOption(
            this IEditorConfigStorageLocation editorConfigStorageLocation,
            AnalyzerConfigOptions analyzerConfigOptions,
            Type type,
            out object value
            )
        {
            // This is a workaround until we have an API for enumeratings AnalyzerConfigOptions. See https://github.com/dotnet/roslyn/issues/41840
            var backingField = analyzerConfigOptions
                               .GetType()
                               .GetField(
                "_backing",
                System.Reflection.BindingFlags.NonPublic
                | System.Reflection.BindingFlags.Instance
                );
            var backing = backingField?.GetValue(analyzerConfigOptions);

            if (backing is IReadOnlyDictionary <string, string> backingDictionary)
            {
                return(editorConfigStorageLocation.TryGetOption(backingDictionary, type, out value));
            }

            value = null;
            return(false);
        }
Exemplo n.º 2
0
        public static bool TryGetOption(this IEditorConfigStorageLocation editorConfigStorageLocation, AnalyzerConfigOptions analyzerConfigOptions, Type type, out object value)
        {
            var optionDictionary = analyzerConfigOptions.Keys.ToImmutableDictionary(
                key => key,
                key =>
            {
                analyzerConfigOptions.TryGetValue(key, out var optionValue);
                return(optionValue);
            });

            return(editorConfigStorageLocation.TryGetOption(optionDictionary, type, out value));
        }
        public static bool TryGetOption(this IEditorConfigStorageLocation editorConfigStorageLocation, AnalyzerConfigOptions analyzerConfigOptions, Type type, out object?value)
        {
            // This is a workaround until we have an API for enumeratings AnalyzerConfigOptions. See https://github.com/dotnet/roslyn/issues/41840
            if (analyzerConfigOptions.GetType().FullName == typeof(DictionaryAnalyzerConfigOptions).FullName)
            {
                var optionsField = analyzerConfigOptions.GetType().GetField(nameof(DictionaryAnalyzerConfigOptions.Options), BindingFlags.NonPublic | BindingFlags.Instance);
                Contract.ThrowIfNull(optionsField);

                var options = optionsField.GetValue(analyzerConfigOptions);
                Contract.ThrowIfNull(options);

                return(editorConfigStorageLocation.TryGetOption((ImmutableDictionary <string, string?>)options, type, out value));
            }

            value = null;
            return(false);
        }