WebExtentions.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Reflection;
  6. using System.Reflection.Emit;
  7. namespace Bowin.Common.Utility
  8. {
  9. public static class WebExtensions
  10. {
  11. public static T ParseTo<T>(this string value)
  12. {
  13. if (Parser<T>.ParseMethod != null)
  14. return Parser<T>.ParseMethod(value);
  15. throw new NotSupportedException();
  16. }
  17. public static bool TryParseTo<T>(this string value, out T result)
  18. {
  19. if (Parser<T>.TryParseMethod != null)
  20. {
  21. return Parser<T>.TryParseMethod(value, out result);
  22. }
  23. throw new NotSupportedException();
  24. }
  25. public static Nullable<T> ParseStrTo<T>(this string value) where T : struct
  26. {
  27. if (value == null) return null;
  28. if (Parser<T>.TryParseMethod != null)
  29. {
  30. T result;
  31. if (Parser<T>.TryParseMethod(value, out result))
  32. {
  33. return result;
  34. }
  35. else
  36. {
  37. return null;
  38. }
  39. }
  40. throw new NotSupportedException();
  41. }
  42. static WebExtensions()
  43. {
  44. Parser<short>.ParseMethod = short.Parse;
  45. Parser<int>.ParseMethod = int.Parse;
  46. Parser<long>.ParseMethod = long.Parse;
  47. Parser<byte>.ParseMethod = byte.Parse;
  48. Parser<ushort>.ParseMethod = ushort.Parse;
  49. Parser<uint>.ParseMethod = uint.Parse;
  50. Parser<ulong>.ParseMethod = ulong.Parse;
  51. Parser<sbyte>.ParseMethod = sbyte.Parse;
  52. Parser<float>.ParseMethod = float.Parse;
  53. Parser<double>.ParseMethod = double.Parse;
  54. Parser<decimal>.ParseMethod = decimal.Parse;
  55. Parser<bool>.ParseMethod = bool.Parse;
  56. Parser<DateTime>.ParseMethod = DateTime.Parse;
  57. Parser<TimeSpan>.ParseMethod = TimeSpan.Parse;
  58. Parser<Guid>.ParseMethod = Guid.Parse;
  59. Parser<short>.TryParseMethod = short.TryParse;
  60. Parser<int>.TryParseMethod = int.TryParse;
  61. Parser<long>.TryParseMethod = long.TryParse;
  62. Parser<byte>.TryParseMethod = byte.TryParse;
  63. Parser<ushort>.TryParseMethod = ushort.TryParse;
  64. Parser<uint>.TryParseMethod = uint.TryParse;
  65. Parser<ulong>.TryParseMethod = ulong.TryParse;
  66. Parser<sbyte>.TryParseMethod = sbyte.TryParse;
  67. Parser<float>.TryParseMethod = float.TryParse;
  68. Parser<double>.TryParseMethod = double.TryParse;
  69. Parser<decimal>.TryParseMethod = decimal.TryParse;
  70. Parser<bool>.TryParseMethod = bool.TryParse;
  71. Parser<DateTime>.TryParseMethod = DateTime.TryParse;
  72. Parser<TimeSpan>.TryParseMethod = TimeSpan.TryParse;
  73. Parser<Guid>.TryParseMethod = Guid.TryParse;
  74. }
  75. private class Parser<T>
  76. {
  77. public delegate T ParseMethodDelegate(string value);
  78. public delegate bool TryParseMethodDelegate(string value, out T result);
  79. private static bool noParseMethod = false;
  80. private static ParseMethodDelegate _parseMethod;
  81. public static ParseMethodDelegate ParseMethod
  82. {
  83. get
  84. {
  85. if (_parseMethod != null)
  86. return _parseMethod;
  87. if (noParseMethod)
  88. return null;
  89. var method = typeof(T).GetMethod("Parse", new Type[] { typeof(string) });
  90. if (method != null && (method.Attributes & MethodAttributes.Static) != 0)
  91. {
  92. DynamicMethod dynamicMethod = new DynamicMethod(typeof(T).FullName + "_Parse", typeof(T), new Type[] { typeof(string) });
  93. var il = dynamicMethod.GetILGenerator();
  94. il.Emit(OpCodes.Ldarg_0);
  95. il.EmitCall(OpCodes.Call, method, null);
  96. il.Emit(OpCodes.Ret);
  97. return _parseMethod = (Parser<T>.ParseMethodDelegate)dynamicMethod.CreateDelegate(typeof(Parser<T>.ParseMethodDelegate));
  98. }
  99. noParseMethod = true;
  100. return null;
  101. }
  102. set
  103. {
  104. _parseMethod = value;
  105. }
  106. }
  107. private static bool noTryParseMethod = false;
  108. private static TryParseMethodDelegate _tryParseMethod;
  109. public static TryParseMethodDelegate TryParseMethod
  110. {
  111. get
  112. {
  113. if (_tryParseMethod != null)
  114. return _tryParseMethod;
  115. if (noTryParseMethod)
  116. return null;
  117. MethodInfo tryParseMethod = typeof(T).GetMethod("TryParse", new Type[] { typeof(string), Type.GetType(typeof(T).FullName + "&") });
  118. if (tryParseMethod != null)
  119. {
  120. DynamicMethod tryParseDm = new DynamicMethod("TryParse", typeof(bool),
  121. new Type[] { typeof(string), Type.GetType(typeof(T).FullName + "&") },
  122. typeof(Parser<T>), false);
  123. ILGenerator tryParseIl = tryParseDm.GetILGenerator();
  124. tryParseIl.Emit(OpCodes.Ldarg_0);
  125. tryParseIl.Emit(OpCodes.Ldarg_1);
  126. tryParseIl.Emit(OpCodes.Call, tryParseMethod);
  127. tryParseIl.Emit(OpCodes.Ret);
  128. return _tryParseMethod = (TryParseMethodDelegate)tryParseDm.CreateDelegate(typeof(TryParseMethodDelegate));
  129. }
  130. noTryParseMethod = true;
  131. return null;
  132. }
  133. set
  134. {
  135. _tryParseMethod = value;
  136. }
  137. }
  138. }
  139. }
  140. }