/// <include file='doc\CultureInfo.uex' path='docs/doc[@for="CultureInfo.Clone"]/*' />
        public virtual Object Clone()
        {
            CultureInfo ci = (CultureInfo)MemberwiseClone();

            //If this is exactly our type, we can make certain optimizations so that we don't allocate NumberFormatInfo or DTFI unless
            //they've already been allocated.  If this is a derived type, we'll take a more generic codepath.
            if (ci.GetType() == typeof(CultureInfo))
            {
                if (dateTimeInfo != null)
                {
                    ci.dateTimeInfo = (DateTimeFormatInfo)dateTimeInfo.Clone();
                }
                if (numInfo != null)
                {
                    ci.numInfo = (NumberFormatInfo)numInfo.Clone();
                }
                ci.m_isReadOnly = false;
            }
            else
            {
                ci.m_isReadOnly   = false;
                ci.DateTimeFormat = (DateTimeFormatInfo)this.DateTimeFormat.Clone();
                ci.NumberFormat   = (NumberFormatInfo)this.NumberFormat.Clone();
            }
            return(ci);
        }
        internal static bool VerifyCultureName(CultureInfo culture, bool throwException)
        {
            BCLDebug.Assert(culture != null, "[CultureInfo.VerifyCultureName]culture!=null");

            //If we have an instance of one of our CultureInfos, the user can't have changed the
            //name and we know that all names are valid in files.
            if (culture.GetType() == typeof(CultureInfo))
            {
                return(true);
            }

            String name = culture.Name;

            for (int i = 0; i < name.Length; i++)
            {
                char c = name[i];
                if (Char.IsLetterOrDigit(c) || c == '-' || c == '_')
                {
                    continue;
                }
                if (throwException)
                {
                    throw new ArgumentException(Environment.GetResourceString("Argument_InvalidResourceCultureName", name));
                }
                return(false);
            }
            return(true);
        }
        public static CultureInfo GetPersianCultureInfo()
        {
            var persianCultureInfo = new CultureInfo("fa-IR");

            SetPersianDateTimeFormatInfo(persianCultureInfo.DateTimeFormat);
            SetNumberFormatInfo(persianCultureInfo.NumberFormat);

            var cal = new PersianCalendar();

            FieldInfo fieldInfo = persianCultureInfo.GetType().GetField("calendar", BindingFlags.NonPublic | BindingFlags.Instance);
            if (fieldInfo != null)
                fieldInfo.SetValue(persianCultureInfo, cal);

            FieldInfo info = persianCultureInfo.DateTimeFormat.GetType().GetField("calendar", BindingFlags.NonPublic | BindingFlags.Instance);
            if (info != null)
                info.SetValue(persianCultureInfo.DateTimeFormat, cal);

            return persianCultureInfo;
        }
        /// <include file='doc\CultureInfo.uex' path='docs/doc[@for="CultureInfo.ReadOnly"]/*' />
        public static CultureInfo ReadOnly(CultureInfo ci)
        {
            if (ci == null)
            {
                throw new ArgumentNullException("ci");
            }
            if (ci.IsReadOnly)
            {
                return(ci);
            }
            CultureInfo info = (CultureInfo)(ci.MemberwiseClone());

            //If this is exactly our type, we can make certain optimizations so that we don't allocate NumberFormatInfo or DTFI unless
            //they've already been allocated.  If this is a derived type, we'll take a more generic codepath.
            if (ci.GetType() == typeof(CultureInfo))
            {
                if (ci.dateTimeInfo != null)
                {
                    info.dateTimeInfo = DateTimeFormatInfo.ReadOnly(ci.dateTimeInfo);
                }
                if (ci.numInfo != null)
                {
                    info.numInfo = NumberFormatInfo.ReadOnly(ci.numInfo);
                }
            }
            else
            {
                info.DateTimeFormat = DateTimeFormatInfo.ReadOnly(ci.DateTimeFormat);
                info.NumberFormat   = NumberFormatInfo.ReadOnly(ci.NumberFormat);
            }
            // Don't set the read-only flag too early.
            // We should set the read-only flag here.  Otherwise, info.DateTimeFormat will not be able to set.
            info.m_isReadOnly = true;

            return(info);
        }
Exemple #5
0
        /// <include file='doc\CultureInfo.uex' path='docs/doc[@for="CultureInfo.ReadOnly"]/*' />        
        public static CultureInfo ReadOnly(CultureInfo ci) {
            if (ci == null) {
                throw new ArgumentNullException("ci");
            }
            if (ci.IsReadOnly) {
                return (ci);
            }
            CultureInfo info = (CultureInfo)(ci.MemberwiseClone());

            //If this is exactly our type, we can make certain optimizations so that we don't allocate NumberFormatInfo or DTFI unless
            //they've already been allocated.  If this is a derived type, we'll take a more generic codepath.
            if (ci.GetType()==typeof(CultureInfo)) {
                if (ci.dateTimeInfo != null) {
                    info.dateTimeInfo = DateTimeFormatInfo.ReadOnly(ci.dateTimeInfo);
                }
                if (ci.numInfo != null) {
                    info.numInfo = NumberFormatInfo.ReadOnly(ci.numInfo);
                }
            } else {
                info.DateTimeFormat = DateTimeFormatInfo.ReadOnly(ci.DateTimeFormat);
                info.NumberFormat = NumberFormatInfo.ReadOnly(ci.NumberFormat);
            }
            // Don't set the read-only flag too early.
            // We should set the read-only flag here.  Otherwise, info.DateTimeFormat will not be able to set.
            info.m_isReadOnly = true;
            
            return (info);
        }
Exemple #6
0
        internal static bool VerifyCultureName(CultureInfo culture, bool throwException) {
            BCLDebug.Assert(culture!=null, "[CultureInfo.VerifyCultureName]culture!=null");

            //If we have an instance of one of our CultureInfos, the user can't have changed the
            //name and we know that all names are valid in files.
            if (culture.GetType()==typeof(CultureInfo)) {
                return true;
            }
            
            String name = culture.Name;
            
            for (int i=0; i<name.Length; i++) {
                char c = name[i];
                if (Char.IsLetterOrDigit(c) || c=='-' || c=='_') {
                    continue;
                }
                if (throwException) {
                    throw new ArgumentException(Environment.GetResourceString("Argument_InvalidResourceCultureName", name));
                }
                return false;
            }
            return true;
        }