CultureNotFoundException.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Runtime.Serialization;
  5. namespace System.Globalization
  6. {
  7. [Serializable]
  8. [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
  9. public class CultureNotFoundException : ArgumentException
  10. {
  11. private readonly string? _invalidCultureName; // unrecognized culture name
  12. private readonly int? _invalidCultureId; // unrecognized culture Lcid
  13. public CultureNotFoundException()
  14. : base(DefaultMessage)
  15. {
  16. }
  17. public CultureNotFoundException(string? message)
  18. : base(message)
  19. {
  20. }
  21. public CultureNotFoundException(string? paramName, string? message)
  22. : base(message, paramName)
  23. {
  24. }
  25. public CultureNotFoundException(string? message, Exception? innerException)
  26. : base(message, innerException)
  27. {
  28. }
  29. public CultureNotFoundException(string? paramName, string? invalidCultureName, string? message)
  30. : base(message, paramName)
  31. {
  32. _invalidCultureName = invalidCultureName;
  33. }
  34. public CultureNotFoundException(string? message, string? invalidCultureName, Exception? innerException)
  35. : base(message, innerException)
  36. {
  37. _invalidCultureName = invalidCultureName;
  38. }
  39. public CultureNotFoundException(string? message, int invalidCultureId, Exception? innerException)
  40. : base(message, innerException)
  41. {
  42. _invalidCultureId = invalidCultureId;
  43. }
  44. public CultureNotFoundException(string? paramName, int invalidCultureId, string? message)
  45. : base(message, paramName)
  46. {
  47. _invalidCultureId = invalidCultureId;
  48. }
  49. protected CultureNotFoundException(SerializationInfo info, StreamingContext context)
  50. : base(info, context)
  51. {
  52. _invalidCultureId = (int?)info.GetValue("InvalidCultureId", typeof(int?));
  53. _invalidCultureName = (string?)info.GetValue("InvalidCultureName", typeof(string));
  54. }
  55. public override void GetObjectData(SerializationInfo info, StreamingContext context)
  56. {
  57. base.GetObjectData(info, context);
  58. info.AddValue("InvalidCultureId", _invalidCultureId, typeof(int?));
  59. info.AddValue("InvalidCultureName", _invalidCultureName, typeof(string));
  60. }
  61. public virtual int? InvalidCultureId => _invalidCultureId;
  62. public virtual string? InvalidCultureName => _invalidCultureName;
  63. private static string DefaultMessage => SR.Argument_CultureNotSupported;
  64. private string? FormattedInvalidCultureId =>
  65. InvalidCultureId != null ?
  66. string.Format(CultureInfo.InvariantCulture, "{0} (0x{0:x4})", (int)InvalidCultureId) :
  67. InvalidCultureName;
  68. public override string Message
  69. {
  70. get
  71. {
  72. string s = base.Message;
  73. if (_invalidCultureId != null || _invalidCultureName != null)
  74. {
  75. string valueMessage = SR.Format(SR.Argument_CultureInvalidIdentifier, FormattedInvalidCultureId);
  76. if (s == null)
  77. {
  78. return valueMessage;
  79. }
  80. return s + Environment.NewLineConst + valueMessage;
  81. }
  82. return s;
  83. }
  84. }
  85. }
  86. }