Exemple #1
0
        private Dispatcher(Type implementation, MethodInfo multiMethod, bool is32BitPlatform)
        {
            Constraints.CheckImplementationType(implementation);
            Constraints.CheckMultiMethod(multiMethod);

            int argumentCount = multiMethod.GetParameters().Length;

            DispatchTable dispatchTable = new DispatchTable(argumentCount, is32BitPlatform);

            Type currentType = implementation;

            while (currentType != typeof(object))
            {
                MethodInfo[] methods = currentType.GetMethods(
                    DefaultBindingFlags | BindingFlags.DeclaredOnly);

                foreach (MethodInfo candidate in methods)
                {
                    if (candidate.MethodHandle != multiMethod.MethodHandle &&
                        candidate.Name == multiMethod.Name &&
                        candidate.GetParameters().Length == argumentCount &&
                        !candidate.ContainsGenericParameters &&
                        !HasInterfaceOrAbstractParameter(candidate))
                    {
                        Type[] types = GetParameterTypes(candidate);

                        //System.Diagnostics.Debug.WriteLine(
                        //    string.Format("{0}.{1}", candidate.DeclaringType, candidate));

                        dispatchTable.InternalAdd(types,
                                                  MethodInvokerHelper.CreateInvoker(candidate));
                    }
                }

                currentType = currentType.BaseType;
            }

            _implementation = implementation;
            _multiMethod    = multiMethod;
            _dispatchTable  = dispatchTable;
        }
Exemple #2
0
        public static MethodInvoker CreateInvoker(MethodInfo info)
        {
            Constraints.CheckMethod(info);

            ParameterInfo[] parameters = info.GetParameters();

            bool action = info.ReturnType == typeof(void);

            Type[] types = null;

            if (action)
            {
                types = new Type[parameters.Length + 1];
            }
            else
            {
                types = new Type[parameters.Length + 2];
            }

            types[0] = info.DeclaringType;

            for (int i = 0; i < parameters.Length; ++i)
            {
                types[i + 1] = parameters[i].ParameterType;
            }

            if (action)
            {
                return((MethodInvoker)actionMaker[parameters.Length]
                       .MakeGenericMethod(types).Invoke(null, new object[] { info }));
            }
            else
            {
                types[parameters.Length + 1] = info.ReturnType;

                return((MethodInvoker)funcMaker[parameters.Length]
                       .MakeGenericMethod(types).Invoke(null, new object[] { info }));
            }
        }