DataReaderConstant.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using Ant.Common;
  2. using Ant.Utility;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace Ant.Extensions
  10. {
  11. static class DataReaderConstant
  12. {
  13. #region
  14. internal static MethodInfo GetReaderMethod(Type type)
  15. {
  16. MethodInfo result;
  17. bool isNullable = false;
  18. Type underlyingType;
  19. if (AntUtils.IsNullable(type, out underlyingType))
  20. {
  21. isNullable = true;
  22. type = underlyingType;
  23. }
  24. if (type.IsEnum)
  25. {
  26. result = (isNullable ? Reader_GetEnum_Nullable : Reader_GetEnum).MakeGenericMethod(type);
  27. return result;
  28. }
  29. TypeCode typeCode = Type.GetTypeCode(type);
  30. switch (typeCode)
  31. {
  32. case TypeCode.Int16:
  33. result = isNullable ? Reader_GetInt16_Nullable : Reader_GetInt16;
  34. break;
  35. case TypeCode.Int32:
  36. result = isNullable ? Reader_GetInt32_Nullable : Reader_GetInt32;
  37. break;
  38. case TypeCode.Int64:
  39. result = isNullable ? Reader_GetInt64_Nullable : Reader_GetInt64;
  40. break;
  41. case TypeCode.Decimal:
  42. result = isNullable ? Reader_GetDecimal_Nullable : Reader_GetDecimal;
  43. break;
  44. case TypeCode.Double:
  45. result = isNullable ? Reader_GetDouble_Nullable : Reader_GetDouble;
  46. break;
  47. case TypeCode.Single:
  48. result = isNullable ? Reader_GetFloat_Nullable : Reader_GetFloat;
  49. break;
  50. case TypeCode.Boolean:
  51. result = isNullable ? Reader_GetBoolean_Nullable : Reader_GetBoolean;
  52. break;
  53. case TypeCode.DateTime:
  54. result = isNullable ? Reader_GetDateTime_Nullable : Reader_GetDateTime;
  55. break;
  56. case TypeCode.Byte:
  57. result = isNullable ? Reader_GetByte_Nullable : Reader_GetByte;
  58. break;
  59. case TypeCode.Char:
  60. result = isNullable ? Reader_GetChar_Nullable : Reader_GetChar;
  61. break;
  62. case TypeCode.String:
  63. result = Reader_GetString;
  64. break;
  65. default:
  66. if (type == UtilConstants.TypeOfGuid)
  67. {
  68. result = isNullable ? Reader_GetGuid_Nullable : Reader_GetGuid;
  69. }
  70. else if (type == UtilConstants.TypeOfObject)
  71. {
  72. result = Reader_GetValue;
  73. }
  74. else
  75. {
  76. result = (isNullable ? Reader_GetTValue_Nullable : Reader_GetTValue).MakeGenericMethod(type);
  77. }
  78. break;
  79. }
  80. return result;
  81. }
  82. #region
  83. internal static readonly MethodInfo Reader_GetInt16 = typeof(DataReaderExtensions).GetMethod("GetInt16");
  84. internal static readonly MethodInfo Reader_GetInt16_Nullable = typeof(DataReaderExtensions).GetMethod("GetInt16_Nullable");
  85. internal static readonly MethodInfo Reader_GetInt32 = typeof(DataReaderExtensions).GetMethod("GetInt32");
  86. internal static readonly MethodInfo Reader_GetInt32_Nullable = typeof(DataReaderExtensions).GetMethod("GetInt32_Nullable");
  87. internal static readonly MethodInfo Reader_GetInt64 = typeof(DataReaderExtensions).GetMethod("GetInt64");
  88. internal static readonly MethodInfo Reader_GetInt64_Nullable = typeof(DataReaderExtensions).GetMethod("GetInt64_Nullable");
  89. internal static readonly MethodInfo Reader_GetDecimal = typeof(DataReaderExtensions).GetMethod("GetDecimal");
  90. internal static readonly MethodInfo Reader_GetDecimal_Nullable = typeof(DataReaderExtensions).GetMethod("GetDecimal_Nullable");
  91. internal static readonly MethodInfo Reader_GetDouble = typeof(DataReaderExtensions).GetMethod("GetDouble");
  92. internal static readonly MethodInfo Reader_GetDouble_Nullable = typeof(DataReaderExtensions).GetMethod("GetDouble_Nullable");
  93. internal static readonly MethodInfo Reader_GetFloat = typeof(DataReaderExtensions).GetMethod("GetFloat");
  94. internal static readonly MethodInfo Reader_GetFloat_Nullable = typeof(DataReaderExtensions).GetMethod("GetFloat_Nullable");
  95. internal static readonly MethodInfo Reader_GetBoolean = typeof(DataReaderExtensions).GetMethod("GetBoolean");
  96. internal static readonly MethodInfo Reader_GetBoolean_Nullable = typeof(DataReaderExtensions).GetMethod("GetBoolean_Nullable");
  97. internal static readonly MethodInfo Reader_GetDateTime = typeof(DataReaderExtensions).GetMethod("GetDateTime");
  98. internal static readonly MethodInfo Reader_GetDateTime_Nullable = typeof(DataReaderExtensions).GetMethod("GetDateTime_Nullable");
  99. internal static readonly MethodInfo Reader_GetGuid = typeof(DataReaderExtensions).GetMethod("GetGuid");
  100. internal static readonly MethodInfo Reader_GetGuid_Nullable = typeof(DataReaderExtensions).GetMethod("GetGuid_Nullable");
  101. internal static readonly MethodInfo Reader_GetByte = typeof(DataReaderExtensions).GetMethod("GetByte");
  102. internal static readonly MethodInfo Reader_GetByte_Nullable = typeof(DataReaderExtensions).GetMethod("GetByte_Nullable");
  103. internal static readonly MethodInfo Reader_GetChar = typeof(DataReaderExtensions).GetMethod("GetChar");
  104. internal static readonly MethodInfo Reader_GetChar_Nullable = typeof(DataReaderExtensions).GetMethod("GetChar_Nullable");
  105. internal static readonly MethodInfo Reader_GetString = typeof(DataReaderExtensions).GetMethod("GetString");
  106. internal static readonly MethodInfo Reader_GetValue = typeof(DataReaderExtensions).GetMethod("GetValue");
  107. internal static readonly MethodInfo Reader_GetEnum = typeof(DataReaderExtensions).GetMethod("GetEnum");
  108. internal static readonly MethodInfo Reader_GetEnum_Nullable = typeof(DataReaderExtensions).GetMethod("GetEnum_Nullable");
  109. internal static readonly MethodInfo Reader_GetTValue = typeof(DataReaderExtensions).GetMethod("GetTValue");
  110. internal static readonly MethodInfo Reader_GetTValue_Nullable = typeof(DataReaderExtensions).GetMethod("GetTValue_Nullable");
  111. #endregion
  112. #endregion
  113. }
  114. }