Пример #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CultureContext"/> class.
        /// </summary>
        /// <param name="cultureName">
        /// The culture.
        /// </param>
        public CultureContext(string cultureName)
        {
            if (string.IsNullOrEmpty(cultureName))
                throw new ArgumentException(@"The culture name cannot be null or empty.", "cultureName");

            _originalContext = currentContext;
            _thread = Thread.CurrentThread;
            _originalCulture = Thread.CurrentThread.CurrentCulture;

            _cultureName = cultureName;
            CultureInfo culture;
            try
            {
                culture = CultureHelper.GetCultureInfo(_cultureName);
            }
            catch (CultureNotFoundException)
            {
                culture = Constants.DefaultCulture;
            }

            Thread.CurrentThread.CurrentCulture = culture;

            currentContext = this;
        }
Пример #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CultureContext"/> class.
        /// </summary>
        /// <param name="culture">
        /// The culture.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// The <paramref name="culture"/> parameter is null.
        /// </exception>
        public CultureContext(CultureInfo culture)
        {
            if (culture == null)
                throw new ArgumentNullException("culture");

            _originalContext = currentContext;
            _thread = Thread.CurrentThread;
            _originalCulture = Thread.CurrentThread.CurrentCulture;

            _cultureName = culture.Name;
            Thread.CurrentThread.CurrentCulture = culture;

            currentContext = this;
        }
Пример #3
0
        public void Dispose()
        {
            if (_disposed)
                return;

            _disposed = true;

            if (_thread != Thread.CurrentThread)
                throw new InvalidOperationException("This CultureContext is being disposed on a different thread than it was created.");

            if (currentContext != this)
                throw new InvalidOperationException("This CultureContext is being disposed out of order.");

            currentContext = _originalContext;
            Thread.CurrentThread.CurrentCulture = _originalCulture;
        }