EntityExtension.cs 2.0 KB

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