StorageableMethodInfo.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace SqlSugar
  8. {
  9. public class StorageableMethodInfo
  10. {
  11. internal SqlSugarProvider Context { get; set; }
  12. internal MethodInfo MethodInfo { get; set; }
  13. internal object objectValue { get; set; }
  14. public int ExecuteCommand()
  15. {
  16. if (Context == null) return 0;
  17. object objectValue = null;
  18. MethodInfo method = GetSaveMethod(ref objectValue);
  19. if (method == null) return 0;
  20. return (int)method.Invoke(objectValue, new object[] { });
  21. }
  22. public StorageableAsMethodInfo AsInsertable
  23. {
  24. get
  25. {
  26. var type = "AsInsertable";
  27. return GetAs(type);
  28. }
  29. }
  30. public StorageableAsMethodInfo AsUpdateable
  31. {
  32. get
  33. {
  34. var type = "AsUpdateable";
  35. return GetAs(type);
  36. }
  37. }
  38. private StorageableAsMethodInfo GetAs(string type)
  39. {
  40. object objectValue = null;
  41. MethodInfo method = GetSaveMethod(ref objectValue);
  42. if (method == null) return new StorageableAsMethodInfo(null);
  43. method = objectValue.GetType().GetMethod("ToStorage");
  44. objectValue = method.Invoke(objectValue, new object[] { });
  45. StorageableAsMethodInfo result = new StorageableAsMethodInfo(type);
  46. result.ObjectValue = objectValue;
  47. result.Method = method;
  48. return result;
  49. }
  50. private MethodInfo GetSaveMethod(ref object callValue)
  51. {
  52. if (objectValue == null)
  53. return null;
  54. callValue = MethodInfo.Invoke(Context, new object[] { objectValue });
  55. return callValue.GetType().GetMyMethod("ExecuteCommand",0);
  56. }
  57. public StorageableMethodInfo ToStorage()
  58. {
  59. return this;
  60. }
  61. public StorageableSplitTableMethodInfo SplitTable()
  62. {
  63. object objectValue = null;
  64. MethodInfo method = GetSaveMethod(ref objectValue);
  65. if (method == null) return new StorageableSplitTableMethodInfo(null);
  66. method = objectValue.GetType().GetMethod("SplitTable");
  67. objectValue = method.Invoke(objectValue, new object[] { });
  68. StorageableSplitTableMethodInfo result = new StorageableSplitTableMethodInfo(null);
  69. result.ObjectValue = objectValue;
  70. result.Method = method;
  71. return result;
  72. }
  73. }
  74. public class StorageableAsMethodInfo
  75. {
  76. private StorageableAsMethodInfo() { }
  77. private string type;
  78. public StorageableAsMethodInfo(string type)
  79. {
  80. this.type = type;
  81. }
  82. internal object ObjectValue { get; set; }
  83. internal MethodInfo Method { get; set; }
  84. public int ExecuteCommand()
  85. {
  86. if (type == null) return 0;
  87. PropertyInfo property = ObjectValue.GetType().GetProperty(type);
  88. var value = property.GetValue(ObjectValue);
  89. var newObj= value.GetType().GetMethod("ExecuteCommand").Invoke(value, new object[] { });
  90. return (int)newObj;
  91. }
  92. }
  93. public class StorageableSplitTableMethodInfo
  94. {
  95. private StorageableSplitTableMethodInfo() { }
  96. private string type;
  97. public StorageableSplitTableMethodInfo(string type)
  98. {
  99. this.type = type;
  100. }
  101. internal object ObjectValue { get; set; }
  102. internal MethodInfo Method { get; set; }
  103. public int ExecuteCommand()
  104. {
  105. var newObj = ObjectValue.GetType().GetMethod("ExecuteCommand").Invoke(ObjectValue, new object[] { });
  106. return (int)newObj;
  107. }
  108. }
  109. }