Advertisement
Guest User

Untitled

a guest
Apr 28th, 2015
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. using System;
  2. using System.Globalization;
  3. using static System.Console;
  4.  
  5. namespace System.Runtime.CompilerServices
  6. {
  7. public class FormattableStringFactory
  8. {
  9. public static IFormattable Create(string messageFormat, params object[] args)
  10. {
  11. return new FormattableString(messageFormat, args);
  12. }
  13.  
  14. public class FormattableString : IFormattable
  15. {
  16. private readonly string _messageFormat;
  17. private readonly object[] _args;
  18.  
  19. public FormattableString(string messageFormat, object[] args)
  20. {
  21. _messageFormat = messageFormat;
  22. _args = args;
  23. }
  24.  
  25. public string ToString(string format, IFormatProvider formatProvider)
  26. {
  27. return string.Format(formatProvider, _messageFormat, _args);
  28. }
  29. }
  30. }
  31. }
  32.  
  33. namespace CsTests
  34. {
  35. public static class FormattableExt
  36. {
  37. public static string AsFr(this IFormattable format)
  38. {
  39. return format.ToString(null, CultureInfo.GetCultureInfo("fr-FR"));
  40. }
  41. }
  42. class Program
  43. {
  44. static string FR(IFormattable src)
  45. {
  46. return src.ToString(null, CultureInfo.GetCultureInfo("fr-FR"));
  47. }
  48.  
  49. static string US(IFormattable src)
  50. {
  51. return src.ToString(null, CultureInfo.GetCultureInfo("en-US"));
  52. }
  53. static string INV(IFormattable src)
  54. {
  55. return src.ToString(null, CultureInfo.InvariantCulture);
  56. }
  57.  
  58. private static void Main(string[] args)
  59. {
  60. WriteLine($"Number: {999 * 2:N}; the current date is {DateTime.Now + TimeSpan.FromHours(12)}");
  61. WriteLine(FR($"Number: {999 * 2:N}; the current date is {DateTime.Now + TimeSpan.FromHours(12)}"));
  62. WriteLine(US($"Number: {999 * 2:N}; the current date is {DateTime.Now + TimeSpan.FromHours(12)}"));
  63. WriteLine(INV($"Number: {999 * 2:N}; the current date is {DateTime.Now + TimeSpan.FromHours(12)}"));
  64. }
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement