UpdateExpressionMethodInfo.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Linq;
  7. namespace SqlSugar
  8. {
  9. public class UpdateExpressionMethodInfo
  10. {
  11. internal SqlSugarProvider Context { get; set; }
  12. internal MethodInfo MethodInfo { get; set; }
  13. internal object objectValue { get; set; }
  14. internal Type Type { get; set; }
  15. public int ExecuteCommand()
  16. {
  17. if (Context == null) return 0;
  18. var result = objectValue.GetType().GetMethod("ExecuteCommand").Invoke(objectValue, new object[] { });
  19. return (int)result;
  20. }
  21. public async Task<int> ExecuteCommandAsync()
  22. {
  23. if (Context == null) return 0;
  24. var result = objectValue.GetType().GetMyMethod("ExecuteCommandAsync", 0).Invoke(objectValue, new object[] { });
  25. return await (Task<int>)result;
  26. }
  27. public UpdateExpressionMethodInfo Where(string expShortName,FormattableString whereExpressionString)
  28. {
  29. var newMethod = objectValue.GetType().GetMyMethod("Where", 1);
  30. var exp = DynamicCoreHelper.GetWhere(Type, expShortName, whereExpressionString);
  31. var result = newMethod.Invoke(objectValue, new object[] { exp });
  32. return new UpdateExpressionMethodInfo()
  33. {
  34. objectValue = result,
  35. Type=this.Type,
  36. Context=this.Context
  37. };
  38. }
  39. public UpdateExpressionMethodInfo SetColumns(string expShortName, FormattableString fieldExpressionString)
  40. {
  41. var newMethod = objectValue.GetType().GetMethods()
  42. .Where(it=>
  43. {
  44. var isTrue= it.Name == "SetColumns" && it.GetParameters().Count() == 1;
  45. if (isTrue)
  46. {
  47. return it.GetParameters().First().ToString().Contains(",System.Boolean");
  48. }
  49. return false;
  50. })
  51. .Single();
  52. var exp1 =DynamicCoreHelper.GetWhere(Type, expShortName, fieldExpressionString);
  53. var result = newMethod.Invoke(objectValue, new object[] { exp1 });
  54. return new UpdateExpressionMethodInfo()
  55. {
  56. objectValue = result,
  57. Type = this.Type,
  58. Context = this.Context
  59. };
  60. }
  61. }
  62. }