Utils.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using Ant.DbExpressions;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. namespace Ant.Oracle
  8. {
  9. internal static class Utils
  10. {
  11. public static void CheckNull(object obj, string paramName = null)
  12. {
  13. if (obj == null)
  14. throw new ArgumentNullException(paramName);
  15. }
  16. public static bool IsNullable(Type type)
  17. {
  18. Type unType;
  19. return IsNullable(type, out unType);
  20. }
  21. public static bool IsNullable(Type type, out Type unType)
  22. {
  23. unType = Nullable.GetUnderlyingType(type);
  24. return unType != null;
  25. }
  26. public static Type GetUnderlyingType(Type type)
  27. {
  28. Type unType;
  29. if (!IsNullable(type, out unType))
  30. unType = type;
  31. return unType;
  32. }
  33. public static bool AreEqual(object obj1, object obj2)
  34. {
  35. if (obj1 == null && obj2 == null)
  36. return true;
  37. if (obj1 != null)
  38. {
  39. return obj1.Equals(obj2);
  40. }
  41. if (obj2 != null)
  42. {
  43. return obj2.Equals(obj1);
  44. }
  45. return object.Equals(obj1, obj2);
  46. }
  47. public static Dictionary<TKey, TValue> Clone<TKey, TValue>(Dictionary<TKey, TValue> source, IEqualityComparer<TKey> comparer = null)
  48. {
  49. Dictionary<TKey, TValue> ret;
  50. if (comparer == null)
  51. ret = new Dictionary<TKey, TValue>(source.Count);
  52. else
  53. ret = new Dictionary<TKey, TValue>(source.Count, comparer);
  54. foreach (var kv in source)
  55. {
  56. ret.Add(kv.Key, kv.Value);
  57. }
  58. return ret;
  59. }
  60. public static string ToMethodString(this MethodInfo method)
  61. {
  62. StringBuilder sb = new StringBuilder();
  63. ParameterInfo[] parameters = method.GetParameters();
  64. for (int i = 0; i < parameters.Length; i++)
  65. {
  66. ParameterInfo p = parameters[i];
  67. if (i > 0)
  68. sb.Append(",");
  69. string s = null;
  70. if (p.IsOut)
  71. s = "out ";
  72. sb.AppendFormat("{0}{1} {2}", s, p.ParameterType.Name, p.Name);
  73. }
  74. return string.Format("{0}.{1}({2})", method.DeclaringType.Name, method.Name, sb.ToString());
  75. }
  76. }
  77. }