3

我使用此代码将我的文化更改为 .Net3.5 中的波斯文化:

if (CultureInfo.CurrentCulture.ToString() == "fa-IR")
        {
            CultureInfo persianCalture = new CultureInfo("fa-IR");
            DateTimeFormatInfo info = persianCalture.DateTimeFormat;
            info.AbbreviatedDayNames = new string[] { "  ی  ", "  د  ", "  س  ", "  چ  ", "  پ  ", "  ج  ", "  ش  " };
            info.DayNames = new string[] { "يکشنبه", "دوشنبه", "سه‌شنبه", "چهارشنبه", "پنجشنبه", "جمعه", "شنبه" };
            info.AbbreviatedMonthNames = new string[] { "فروردين", "ارديبهشت", "خرداد", "تير", "مرداد", "شهريور", "مهر", "آبان", "آذر", "دی", "بهمن", "اسفند", "" };
            info.MonthNames = new string[] { "فروردين", "ارديبهشت", "خرداد", "تير", "مرداد", "شهريور", "مهر", "آبان", "آذر", "دی", "بهمن", "اسفند", "" };
            info.AbbreviatedMonthGenitiveNames = new string[] { "فروردين", "ارديبهشت", "خرداد", "تير", "مرداد", "شهريور", "مهر", "آبان", "آذر", "دی", "بهمن", "اسفند", "" };
            info.AMDesignator = "صبح";
            info.PMDesignator = "عصر";
            info.ShortDatePattern = "yyyy/MM/dd";
            info.FullDateTimePattern = "yyyy/MM/dd hh:mm:ss";
            info.LongDatePattern = "yyyy MMMM dd dddd";
            info.FirstDayOfWeek = DayOfWeek.Saturday;
            persianCalture.DateTimeFormat = info;
            PersianCalendar PersianCal = new PersianCalendar();

            typeof(DateTimeFormatInfo).GetField("calendar", (System.Reflection.BindingFlags.NonPublic | (System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance))).SetValue(info, PersianCal);
            typeof(CultureInfo).GetField("calendar", (System.Reflection.BindingFlags.NonPublic | (System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance))).SetValue(persianCalture, PersianCal);
            Thread.CurrentThread.CurrentCulture = persianCalture;
            Thread.CurrentThread.CurrentUICulture = persianCalture;
            CultureInfo.CurrentCulture.DateTimeFormat = info;
            CultureInfo.CurrentUICulture.DateTimeFormat = info;
        }

但下面的行是抛出 ArgumentNullException :

System.Globalization.DateTimeFormatInfo.calendar

        typeof(DateTimeFormatInfo).GetField("calendar", (System.Reflection.BindingFlags.NonPublic | (System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance))).SetValue(info, PersianCal);
        typeof(CultureInfo).GetField("calendar", (System.Reflection.BindingFlags.NonPublic | (System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance))).SetValue(persianCalture, PersianCal);

有谁知道为什么会抛出这个异常?

感谢您的任何帮助,您可以提供。

4

1 回答 1

0

尝试使用:typeof(DateTimeFormatInfo).GetProperty("Calendar"....)

DateTimeFormatInfo 有一个 Calendar 属性(大写 C)。

希望这可以帮助,

谢。

于 2012-07-09T12:24:12.343 回答