Skip to content

Commit cab4f7b

Browse files
committed
Only work around the GetText bug prior to Roslyn 1.2
1 parent 683c138 commit cab4f7b

1 file changed

Lines changed: 20 additions & 9 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers/Settings/SettingsHelper.cs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,20 @@ internal static class SettingsHelper
2323
{
2424
private const string SettingsFileName = "stylecop.json";
2525

26+
private static readonly bool AvoidAdditionalTextGetText;
27+
2628
private static readonly ConcurrentDictionary<Type, ConcurrentDictionary<string, FieldInfo>> FieldInfos =
2729
new ConcurrentDictionary<Type, ConcurrentDictionary<string, FieldInfo>>();
2830

2931
private static readonly ConcurrentDictionary<Type, ConcurrentDictionary<string, PropertyInfo>> PropertyInfos =
3032
new ConcurrentDictionary<Type, ConcurrentDictionary<string, PropertyInfo>>();
3133

34+
static SettingsHelper()
35+
{
36+
// dotnet/roslyn#6596 was fixed for Roslyn 1.2
37+
AvoidAdditionalTextGetText = typeof(AdditionalText).GetTypeInfo().Assembly.GetName().Version < new Version(1, 2, 0, 0);
38+
}
39+
3240
/// <summary>
3341
/// Gets the StyleCop settings.
3442
/// </summary>
@@ -83,19 +91,22 @@ private static StyleCopSettings GetStyleCopSettings(ImmutableArray<AdditionalTex
8391
/// <returns>The content of the additional text file.</returns>
8492
private static SourceText GetText(AdditionalText additionalText, CancellationToken cancellationToken)
8593
{
86-
object document = GetField(additionalText, "_document");
87-
if (document != null)
94+
if (AvoidAdditionalTextGetText)
8895
{
89-
object textSource = GetField(document, "textSource");
90-
if (textSource != null)
96+
object document = GetField(additionalText, "_document");
97+
if (document != null)
9198
{
92-
object textAndVersion = CallMethod(textSource, "GetValue", new[] { typeof(CancellationToken) }, cancellationToken);
93-
if (textAndVersion != null)
99+
object textSource = GetField(document, "textSource");
100+
if (textSource != null)
94101
{
95-
SourceText text = GetProperty(textAndVersion, "Text") as SourceText;
96-
if (text != null)
102+
object textAndVersion = CallMethod(textSource, "GetValue", new[] { typeof(CancellationToken) }, cancellationToken);
103+
if (textAndVersion != null)
97104
{
98-
return text;
105+
SourceText text = GetProperty(textAndVersion, "Text") as SourceText;
106+
if (text != null)
107+
{
108+
return text;
109+
}
99110
}
100111
}
101112
}

0 commit comments

Comments
 (0)