Beispiel #1
0
            public override MethodBase SelectMethod(BindingFlags bindingAttr, MethodBase[] match, Type[] types, ParameterModifier[] modifiers)
            {
                MethodBase[] methodCandidates;
                this.SelectMethodCalled = true;

                if (this.genericTypeArguments.Length > 0)
                {
                    // Accept only generic methods which can be successfully instantiated w/ these parameters
                    Collection<MethodBase> methods = new Collection<MethodBase>();
                    foreach (MethodBase method in match)
                    {
                        // Must be a MethodInfo because we've already filtered out constructors                            
                        MethodInfo instantiatedMethod = Instantiate((MethodInfo)method, this.genericTypeArguments);
                        if (instantiatedMethod != null)
                        {
                            methods.Add(instantiatedMethod);
                        }
                    }
                    methodCandidates = methods.ToArray();
                }
                else
                {
                    // Accept only candidates which are already instantiated
                    methodCandidates = match.Where(m => m.ContainsGenericParameters == false).ToArray();
                }

                if (methodCandidates.Length == 0)
                {
                    return null;
                }

                // Methods declared on this.declaringType class get top priority as matches
                Type declaringType = this.declaringType;
                MethodBase result = null;
                do
                {
                    MethodBase[] methodsDeclaredHere = methodCandidates.Where(mb => mb.DeclaringType == declaringType).ToArray();
                    if (methodsDeclaredHere.Length > 0)
                    {
                        // Try to find a match
                        result = FindMatch(methodsDeclaredHere, bindingAttr, types, modifiers);
                    }
                    declaringType = declaringType.BaseType;
                }
                while (declaringType != null && result == null); // short-circuit as soon as we find a match

                return result; // returns null if no match found                
            }