This CultureScope class has been designed to enable the running of code segment a user under different Culture to that which is set for the application. The typical example is testing Culture specific code in unit tests. This class by design implements the IDisposable interface allowing the context to automatically revert with a using statement. For more information on the CultureInfo class see http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo(vs.71).aspx

Namespace:  CA.Common.Culture
Assembly:  CA.Common (in CA.Common.dll) Version: 1.0.0.0 (1.0.0.0)

Remarks

Using the dispose method makes it possible to use the CultureScope class with a using class in c#
CopyQuick Example of using CultureScope class using the C# using statement
using (new CultureScope("en-NZ")))
{ 
 //.. do work with New Zealand Culture here
}    
// reverted back
CopyFull source code for the CultureScope class
//Source code from the Code Associate C# code library, Full documentation and latest updates can be found
//@ http://www.codeassociate.com/caapi/
using System;
using System.Globalization;
using System.Threading;

namespace CA.Common.Culture
{
    public class CultureScope : IDisposable  
    {
        private CultureInfo _rollbackCulture;

        private void CommonConstructor(CultureInfo cultureinfo)
        {
            if (cultureinfo != null)
            {
                _rollbackCulture = Thread.CurrentThread.CurrentCulture;
                Thread.CurrentThread.CurrentCulture = cultureinfo;
            }
            else
            {
                throw new ArgumentNullException("cultureinfo", "Cannot set a null CultureInfo to the current thread");
            }
        }
        public CultureScope(CultureInfo cultureinfo)
        {
            CommonConstructor(cultureinfo);
        }

        public CultureScope(string culture) 
        {
            CultureInfo cultureinfo = new CultureInfo(culture);
            CommonConstructor(cultureinfo);
        }


        public void Dispose()
        {
            Thread.CurrentThread.CurrentCulture = _rollbackCulture;
        }

    }
}

Examples

CopyUnit Tests source code showing useage of the CultureScope class
//Source code from the Code Associate C# code library, Full documentation and latest updates can be found
//@ http://www.codeassociate.com/caapi/
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using CA.Common.Culture;
using NUnit.Framework;

namespace CA.Common.UnitTest.Culture
{
    [TestFixture]
    public class CultureScope_UnitTest
    {

        [Test]
        public void BasicCultureScopeTest()
        {
            // Set formatString for a currency so we can test diffrent Cultures 
            string formatString = "{0:c}";
            double TestNumber = 123456.78;

            // get a starting point for whatever Culture is currently the default. 
            string startingresult = string.Format(formatString, TestNumber);

            // set the TestCulture to New Zealand 
            using (new CultureScope("en-NZ"))
            {
                string result = string.Format(formatString, TestNumber);
                // here we assert that the result is formatted with a dollar $
                Assert.AreEqual("$123,456.78", result);
            }
            // 
            // set the TestCulture to English - United Kingdom
            using (new CultureScope("en-GB"))
            {
                // now make the same call as above with the same parameters.. 
                string result = string.Format(formatString, TestNumber);
                // Now assert the string is formatted with pound �
                Assert.AreEqual("�123,456.78", result);
            }

            // get a post test the Culture should be the same as the start
           string postCultureScopeResult = string.Format(formatString, TestNumber);
            // assert this is the same as the starting value
           Assert.AreEqual(startingresult, postCultureScopeResult);
        }

        [Test]
        public void BasicCultureScopeTestUsingTheCultureClass()
        {
            // Set formatString for a currency so we can test diffrent Cultures 
            string formatString = "{0:c}";
            double TestNumber = 123456.78;

            // get a starting point for whatever Culture is currently the default. 
            string startingresult = string.Format(formatString, TestNumber);

            // set the TestCulture to New Zealand 
            using (new CultureScope(new CultureInfo("en-NZ")))
            {
                string result = string.Format(formatString, TestNumber);
                // here we assert that the result is formatted with a dollar $
                Assert.AreEqual("$123,456.78", result);
            }
            // 
            // set the TestCulture to English - United Kingdom
            using (new CultureScope(new CultureInfo("en-GB")))
            {
                // now make the same call as above with the same parameters.. 
                string result = string.Format(formatString, TestNumber);
                // Now assert the string is formatted with pound �
                Assert.AreEqual("�123,456.78", result);
            }

            // get a post test the Culture should be the same as the start
            string PostCultureScopeResult = string.Format(formatString, TestNumber);
            // assert this is the same as the starting value
            Assert.AreEqual(startingresult, PostCultureScopeResult);
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestNullCultureInfoException()
        {
            int i = 0;
            using (new CultureScope((CultureInfo)null))
            {
                // do nothing the statement above will throw an ArgumentNullException 
            }

        }
    }
}

Inheritance Hierarchy

System..::.Object
  CA.Common.Culture..::.CultureScope

See Also