Guest User

Untitled

a guest
Sep 24th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. public class Cache
  2. {
  3. static readonly Dictionary<string, object> _cache = new Dictionary<string, object>();
  4.  
  5. public static T Run<T>(Func<T> func, params object[] prms)
  6. {
  7. var key = GetKey(func, prms);
  8.  
  9. if (!_cache.ContainsKey(key) || !(_cache[key] is T))
  10. _cache.Add(key, func());
  11.  
  12. return (T)_cache[key];
  13. }
  14.  
  15. private static string GetKey<T>(Func<T> func, params object[] prms)
  16. {
  17. var method = func.Method.DeclaringType + "." + func.Method.Name;
  18. var type = typeof(T).FullName;
  19.  
  20. return string.Format("{0}_{1}_{2}", method, type, string.Join("_", prms));
  21. }
  22. }
Add Comment
Please, Sign In to add comment