EntityExtension.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. 
  2. using Infrastructure.Extensions;
  3. using Microsoft.AspNetCore.Http;
  4. using System;
  5. using System.Reflection;
  6. namespace Infrastructure
  7. {
  8. public static class EntityExtension
  9. {
  10. public static TSource ToCreate<TSource>(this TSource source, HttpContext? context = null)
  11. {
  12. var types = source?.GetType();
  13. if (types == null || context == null) return source;
  14. BindingFlags flag = BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance;
  15. types.GetProperty("CreateTime", flag)?.SetValue(source, DateTime.Now, null);
  16. types.GetProperty("AddTime", flag)?.SetValue(source, DateTime.Now, null);
  17. types.GetProperty("CreateBy", flag)?.SetValue(source, context.GetName(), null);
  18. types.GetProperty("Create_by", flag)?.SetValue(source, context.GetName(), null);
  19. //types.GetProperty("UserId", flag)?.SetValue(source, context.GetUId(), null);
  20. types.GetProperty("DeptId", flag)?.SetValue(source, context.GetDeptId(), null);
  21. return source;
  22. }
  23. public static TSource ToUpdate<TSource>(this TSource source, HttpContext? context = null)
  24. {
  25. var types = source?.GetType();
  26. if (types == null || context == null) return source;
  27. BindingFlags flag = BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance;
  28. types.GetProperty("UpdateTime", flag)?.SetValue(source, DateTime.Now, null);
  29. types.GetProperty("Update_time", flag)?.SetValue(source, DateTime.Now, null);
  30. types.GetProperty("UpdateBy", flag)?.SetValue(source, context.GetName(), null);
  31. types.GetProperty("Update_by", flag)?.SetValue(source, context.GetName(), null);
  32. return source;
  33. }
  34. }
  35. }