Skip to content

Instantly share code, notes, and snippets.

@IUntyped
Last active March 26, 2016 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IUntyped/03d09f70c694b5481fbd to your computer and use it in GitHub Desktop.
Save IUntyped/03d09f70c694b5481fbd to your computer and use it in GitHub Desktop.
ForceCulture class
using System;
using System.Globalization;
using System.Threading;
namespace nPacker.@gmail.com
{
public class ForceCulture : IDisposable
{
/// <summary>
/// Constructor
/// </summary>
/// <param name="CultureInfo">CultureInfo to force</param>
public ForceCulture(CultureInfo culture)
{
oldCultureInfo = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = culture;
}
public ForceCulture() : this(CultureInfo.InvariantCulture)
{
}
#region IDisposable
private CultureInfo oldCultureInfo;
///<summary>
/// Finalizer, IDisposable support
///</summary>
~ForceCulture()
{
Dispose(false);
}
///<summary>
/// IDisposable support
///</summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
///<summary>
/// IDisposable support
///</summary>
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
Thread.CurrentThread.CurrentCulture = oldCultureInfo;
}
}
#endregion
}
}
@IUntyped
Copy link
Author

Usage:

  using (new ForceCulture())
  {
    // ... culture-dependent stuff ...
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment