DelegateCache.cs 643 B

1234567891011121314151617181920212223242526
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Reflection;
  4. namespace ARMeilleure.Translation
  5. {
  6. static class DelegateCache
  7. {
  8. private static ConcurrentDictionary<string, Delegate> _delegates;
  9. static DelegateCache()
  10. {
  11. _delegates = new ConcurrentDictionary<string, Delegate>();
  12. }
  13. public static Delegate GetOrAdd(Delegate dlg)
  14. {
  15. return _delegates.GetOrAdd(GetKey(dlg.Method), (key) => dlg);
  16. }
  17. private static string GetKey(MethodInfo info)
  18. {
  19. return $"{info.DeclaringType.FullName}.{info.Name}";
  20. }
  21. }
  22. }