aboutsummaryrefslogtreecommitdiff
path: root/ARMeilleure/Translation/DelegateCache.cs
blob: 7328c61a67d4acb1e897265b0f4ed676d7fc73c1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
using System;
using System.Collections.Concurrent;
using System.Reflection;

namespace ARMeilleure.Translation
{
    static class DelegateCache
    {
        private static ConcurrentDictionary<string, Delegate> _delegates;

        static DelegateCache()
        {
            _delegates = new ConcurrentDictionary<string, Delegate>();
        }

        public static Delegate GetOrAdd(Delegate dlg)
        {
            return _delegates.GetOrAdd(GetKey(dlg.Method), (key) => dlg);
        }

        private static string GetKey(MethodInfo info)
        {
            return $"{info.DeclaringType.FullName}.{info.Name}";
        }
    }
}