Esempio n. 1
0
        /// <summary>
        /// Finds the requested method from a list of provided methods
        /// </summary>
        /// <param name="staticMethod">indicates whether to look for a static method</param>
        /// <param name="oargs">the arguments to pass to the method</param>
        /// <param name="methods">the pre-filtered list of methods that might be capable</param>
        /// <param name="types">the parameter types for the method</param>
        /// <param name="args">the original arguments passed to the method</param>
        /// <returns>a methodinfo if one is found or null</returns>
        internal static MethodBuffer FindMethodFromArray(bool staticMethod, ICollection <MethodBuffer> methods, Type[] types,
                                                         object[] args, out object[] oargs)
        {
            oargs = args;
            MethodBuffer retVal =
                methods.FirstOrDefault(
                    n => EqualSignatures(n.MethodInfo.GetParameters(), types) && (n.MethodInfo.IsStatic == (staticMethod ^ n.IsExtension)));

            if (retVal != null)
            {
                ParameterInfo[] parameters = retVal.MethodInfo.GetParameters();
#if (UseDelegates)
                oargs = EnrichWithOptionalParameters(oargs, parameters, retVal.ArgumentsRaw /*, staticMethod*/);
#else
                oargs = EnrichWithOptionalParameters(oargs, parameters, retVal.ArgumentsRaw);
#endif
            }
            else
            {
                foreach (MethodBuffer m in methods)
                {
#if UseDelegates
                    if (MakeCapableMethodArguments(m.MethodInfo.GetParameters(), types, args, m.ArgumentsRaw /*, staticMethod*/))
#else
                    object[] rawArgs = m.ArgumentsRaw;
                    if (MakeCapableMethodArguments(m.MethodInfo.GetParameters(), types, args, rawArgs))
#endif
                    {
                        oargs  = rawArgs;
                        retVal = m;
                        break;
                    }
                }
            }

            if (retVal != null)
            {
                ParameterInfo[] rawParameters = retVal.MethodInfo.GetParameters();
                Type            delegateType  = typeof(Delegate);
                for (int i = 0; i < rawParameters.Length; i++)
                {
                    if (delegateType.IsAssignableFrom(rawParameters[i].ParameterType) && oargs[i] is FunctionLiteral)
                    {
                        oargs[i] = ((FunctionLiteral)oargs[i]).CreateDelegate(rawParameters[i].ParameterType);
                    }
                }
            }

            return(retVal);
        }
Esempio n. 2
0
        private static MethodBuffer[] SelectGenerics(IEnumerable <MethodInfo> methods, Type[] typeArguments,
                                                     bool extensions)
        {
            return(methods.Select(n =>
            {
#if UseDelegates
                MethodBuffer mi = new MethodBuffer {
                    MethodInfo = n, IsGeneric =
                        n.ContainsGenericParameters, ArgumentsRaw = new object[n.GetParameters().Length /*+(n.IsStatic?0:1)*/]
                };
#else
                MethodBuffer mi = new MethodBuffer
                {
                    MethodInfo = n,
                    IsGeneric = n.ContainsGenericParameters,
                    ArgumentsLength = n.GetParameters().Length,
                    IsExtension = extensions
                };
#endif
                if (mi.IsGeneric && typeArguments != null)
                {
                    try
                    {
                        mi.MethodInfo = n.MakeGenericMethod(typeArguments);
                    }
                    catch (Exception ex)
                    {
                        LogEnvironment.LogEvent(ex.OutlineException(), LogSeverity.Error);
                    }
                }
#if UseDelegates
                mi.Delegate = MakeDelegate(mi.MethodInfo);//Delegate.CreateDelegate(typeof(Dummy), mi.MethodInfo);
                if (mi.Delegate != null)
                {
                    return mi;
                }

                return null;
#else
                return mi;
#endif
            }).Where(n => n != null).ToArray());
        }