我使用此代码在.Net3.5中将我的文化转换为persianculture:
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);
有人知道为什么会抛出此异常吗?
感谢您提供的任何帮助。
答案 0 :(得分:0)
尝试使用:typeof(DateTimeFormatInfo).GetProperty("Calendar"....)
DateTimeFormatInfo具有Calendar属性(Capital C)。
希望这有帮助,
晒。