FormattableString.cs 3.5 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. /*============================================================
  5. **
  6. **
  7. **
  8. ** Purpose: implementation of the FormattableString
  9. ** class.
  10. **
  11. ===========================================================*/
  12. namespace System
  13. {
  14. /// <summary>
  15. /// A composite format string along with the arguments to be formatted. An instance of this
  16. /// type may result from the use of the C# or VB language primitive "interpolated string".
  17. /// </summary>
  18. public abstract class FormattableString : IFormattable
  19. {
  20. /// <summary>
  21. /// The composite format string.
  22. /// </summary>
  23. public abstract string Format { get; }
  24. /// <summary>
  25. /// Returns an object array that contains zero or more objects to format. Clients should not
  26. /// mutate the contents of the array.
  27. /// </summary>
  28. public abstract object[] GetArguments();
  29. /// <summary>
  30. /// The number of arguments to be formatted.
  31. /// </summary>
  32. public abstract int ArgumentCount { get; }
  33. /// <summary>
  34. /// Returns one argument to be formatted from argument position <paramref name="index"/>.
  35. /// </summary>
  36. public abstract object GetArgument(int index);
  37. /// <summary>
  38. /// Format to a string using the given culture.
  39. /// </summary>
  40. public abstract string ToString(IFormatProvider formatProvider);
  41. string IFormattable.ToString(string ignored, IFormatProvider formatProvider)
  42. {
  43. return ToString(formatProvider);
  44. }
  45. /// <summary>
  46. /// Format the given object in the invariant culture. This static method may be
  47. /// imported in C# by
  48. /// <code>
  49. /// using static System.FormattableString;
  50. /// </code>.
  51. /// Within the scope
  52. /// of that import directive an interpolated string may be formatted in the
  53. /// invariant culture by writing, for example,
  54. /// <code>
  55. /// Invariant($"{{ lat = {latitude}; lon = {longitude} }}")
  56. /// </code>
  57. /// </summary>
  58. public static string Invariant(FormattableString formattable)
  59. {
  60. if (formattable == null)
  61. {
  62. throw new ArgumentNullException(nameof(formattable));
  63. }
  64. return formattable.ToString(Globalization.CultureInfo.InvariantCulture);
  65. }
  66. /// <summary>
  67. /// Format the given object in the current culture. This static method may be
  68. /// imported in C# by
  69. /// <code>
  70. /// using static System.FormattableString;
  71. /// </code>.
  72. /// Within the scope
  73. /// of that import directive an interpolated string may be formatted in the
  74. /// current culture by writing, for example,
  75. /// <code>
  76. /// CurrentCulture($"{{ lat = {latitude}; lon = {longitude} }}")
  77. /// </code>
  78. /// </summary>
  79. public static string CurrentCulture(FormattableString formattable)
  80. {
  81. if (formattable == null)
  82. {
  83. throw new ArgumentNullException(nameof(formattable));
  84. }
  85. return formattable.ToString(Globalization.CultureInfo.CurrentCulture);
  86. }
  87. public override string ToString()
  88. {
  89. return ToString(Globalization.CultureInfo.CurrentCulture);
  90. }
  91. }
  92. }