Skip to content

Commit 663b52a

Browse files
committed
Make sure culture info is persisted across methods executed from classes with UseCulture attribute
When we change the current thread's culture, that change is lost when an async method is called. So Microsoft guys have introduced the CultureInfo.DefaultThreadCurrentCulture property in .NET 4.5. See https://stackoverflow.com/a/30664385/1396155 According to this SO answer, we do not have to use this property in .NET 4.6 because async methods seem to get the same culture info the caller method has. So if someday the test project's target framework is updated, feel free to revert this commit.
1 parent 7cf7332 commit 663b52a

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/Helpers/UseCultureAttribute.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ public class UseCultureAttribute : BeforeAfterTestAttribute
3333

3434
private CultureInfo originalUiCulture;
3535

36+
private CultureInfo originalDefaultCulture;
37+
38+
private CultureInfo originalDefaultUiCulture;
39+
3640
/// <summary>
3741
/// Initializes a new instance of the <see cref="UseCultureAttribute"/>
3842
/// class with a culture.
@@ -85,9 +89,14 @@ public override void Before(MethodInfo methodUnderTest)
8589
{
8690
this.originalCulture = Thread.CurrentThread.CurrentCulture;
8791
this.originalUiCulture = Thread.CurrentThread.CurrentUICulture;
92+
this.originalDefaultCulture = CultureInfo.DefaultThreadCurrentCulture;
93+
this.originalDefaultUiCulture = CultureInfo.DefaultThreadCurrentUICulture;
8894

8995
Thread.CurrentThread.CurrentCulture = this.Culture;
9096
Thread.CurrentThread.CurrentUICulture = this.UiCulture;
97+
98+
CultureInfo.DefaultThreadCurrentCulture = this.Culture;
99+
CultureInfo.DefaultThreadCurrentUICulture = this.UiCulture;
91100
}
92101

93102
/// <summary>
@@ -99,6 +108,9 @@ public override void After(MethodInfo methodUnderTest)
99108
{
100109
Thread.CurrentThread.CurrentCulture = this.originalCulture;
101110
Thread.CurrentThread.CurrentUICulture = this.originalUiCulture;
111+
112+
CultureInfo.DefaultThreadCurrentCulture = this.originalDefaultCulture;
113+
CultureInfo.DefaultThreadCurrentUICulture = this.originalDefaultUiCulture;
102114
}
103115
}
104116
}

0 commit comments

Comments
 (0)