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);
        }
        private static ImmutableDictionary <string, string> ReadOptions(AnalyzerConfigOptions analyzerConfigOptions)
        {
            var backing = analyzerConfigOptions.GetType()
                          .GetFields(BindingFlags.NonPublic | BindingFlags.Instance)
                          .FirstOrDefault(g => g.Name == "_backing");

            return(backing.GetValue(analyzerConfigOptions) as ImmutableDictionary <string, string>);
        }
        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);
        }
Beispiel #4
0
            // Local functions.
            static ImmutableDictionary <string, string>?TryGetBackingOptionsDictionary(AnalyzerConfigOptions analyzerConfigOptions)
            {
                // Reflection based optimization for analyzer config options.
                // Ideally 'AnalyzerConfigOptions' would expose such an the API.
                var type = analyzerConfigOptions.GetType();
                const BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic;

                return(type.GetField("_backing", flags)?.GetValue(analyzerConfigOptions) as ImmutableDictionary <string, string> ??
                       type.GetField("_analyzerOptions", flags)?.GetValue(analyzerConfigOptions) as ImmutableDictionary <string, string>);
            }