2012-07-09 81 views
3

我用這個代碼來改變我的文化.NET3.5到persianculture:System.Globalization.DateTimeFormatInfo.calendar原因拋出一個異常

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:

系統.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

嘗試使用:typeof(DateTimeFormatInfo).GetProperty("Calendar"....)

的DateTimeFormatInfo具有日曆屬性(首都C)。

希望這有助於

曬。

+0

這並不能解決我的問題:( – atabrizi 2012-07-14 06:39:02