Пример #1
0
        private static Invoker TryCreateInstanceMethod(MethodInfo method)
        {
            if (method.DeclaringType == null)
            {
                return(null);
            }

            // try get from cache
            var invoker = default(Invoker);

            lock (InstanceMethods)
                if (InstanceMethods.TryGetValue(method, out invoker))
                {
                    return(invoker);
                }

            var creatorsByParamsCount = default(Dictionary <MethodCallSignature, InvokeOperationCreator>[]);

            lock (InstanceMethodCreators)
                if (InstanceMethodCreators.TryGetValue(method.DeclaringType, out creatorsByParamsCount) == false || creatorsByParamsCount == null)
                {
                    goto cacheAndReturn;
                }

            var methodCallSignature = new MethodCallSignature(method);

            if (creatorsByParamsCount.Length < methodCallSignature.Count || creatorsByParamsCount[methodCallSignature.Count] == null)
            {
                goto cacheAndReturn;
            }

            var creatorsBySignature = creatorsByParamsCount[methodCallSignature.Count];
            var creator             = default(InvokeOperationCreator);

            lock (creatorsBySignature)
                if (creatorsBySignature.TryGetValue(methodCallSignature, out creator) == false || creator == null)
                {
                    goto cacheAndReturn;
                }

            invoker = creator(method, method.GetParameters());

cacheAndReturn:
            // cache it
            lock (InstanceMethods)
                InstanceMethods[method] = invoker;

            return(invoker);
        }
Пример #2
0
        public static void RegisterInstanceMethod <InstanceT, ResultT>()
        {
            const int PARAMS_INDEX = 0;

            var creatorsByParamsCount = default(Dictionary <MethodCallSignature, InvokeOperationCreator>[]);

            lock (InstanceMethodCreators)
                if (InstanceMethodCreators.TryGetValue(typeof(InstanceT), out creatorsByParamsCount) == false)
                {
                    InstanceMethodCreators[typeof(InstanceT)] = creatorsByParamsCount = new Dictionary <MethodCallSignature, InvokeOperationCreator> [4];
                }

            var creatorsBySignature = creatorsByParamsCount[PARAMS_INDEX];

            if (creatorsBySignature == null)
            {
                creatorsByParamsCount[PARAMS_INDEX] = creatorsBySignature = new Dictionary <MethodCallSignature, InvokeOperationCreator>();
            }

            var methodCallSignature = new MethodCallSignature(typeof(ResultT));

            lock (creatorsBySignature)
                creatorsBySignature[methodCallSignature] = TryCreate <InstanceT, ResultT>;
        }