Пример #1
0
        protected TMemberInfo MatchMethod(Type[] args, IEnumerable <TMemberInfo> members)
        {
            LinkedList <MethodMatch> matches = new LinkedList <MethodMatch>();

            foreach (var m in members)
            {
                matches.AddLast(
                    new MethodMatch()
                {
                    method     = m,
                    parameters = Array.ConvertAll <ParameterInfo, Type>(
                        m.GetParameters(),
                        p => p.ParameterType
                        ),
                    weight = 0
                }
                    );
            }


            if (args != null)
            {
                for (int i = 0; i < args.Length; i++)
                {
                    Type t = args[i];
                    for (var node = matches.First; node != null;)
                    {
                        var nextNode = node.Next;
                        if (t != null)
                        {
                            Type paramType = node.Value.parameters[i];
                            if (t.Equals(paramType))
                            {
                                node.Value.weight += 1;
                            }
                            else if (typeof(Delegate).IsAssignableFrom(paramType) && typeof(JsFunction).IsAssignableFrom(t))
                            {
                                // we can assing a js function to a delegate
                            }
                            else if (!m_marshaller.IsAssignable(paramType, t))
                            {
                                matches.Remove(node);
                            }
                        }
                        else
                        {
                            // we can't assign undefined or null values to a value types
                            if (node.Value.parameters[i].IsValueType)
                            {
                                matches.Remove(node);
                            }
                        }
                        node = nextNode;
                    }
                }
            }

            MethodMatch best = null;

            foreach (var match in matches)
            {
                best = best == null ? match : (best.weight < match.weight ? match : best);
            }

            return(best == null ? null : best.method);
        }