gpt4 book ai didi

asp.net - System.Globalization.DateTimeFormatInfo.calendar 导致抛出异常

转载 作者:行者123 更新时间:2023-12-05 01:16:29 26 4
gpt4 key购买 nike

我使用此代码将我的文化更改为 .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);

有人知道为什么抛出这个异常吗?

感谢您提供的任何帮助。

最佳答案

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

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

希望对您有所帮助,

晒。

关于asp.net - System.Globalization.DateTimeFormatInfo.calendar 导致抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11394259/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com