CurrentContext.cs 601 B

123456789101112131415161718192021222324252627282930313233
  1. using System.Threading;
  2. namespace Masuit.Tools.Systems;
  3. public class CurrentContext<T>
  4. {
  5. private static readonly AsyncLocal<T> Current = new();
  6. public static void SetData(T value)
  7. {
  8. Current.Value = value;
  9. }
  10. public static T GetData()
  11. {
  12. return Current.Value;
  13. }
  14. }
  15. public class CurrentContext
  16. {
  17. private static readonly AsyncLocal<object> Current = new();
  18. public static void SetData<T>(T value)
  19. {
  20. Current.Value = value;
  21. }
  22. public static T GetData<T>()
  23. {
  24. return Current.Value is T value ? value : default;
  25. }
  26. }