int FindIndexer(Type type, Expression[] args, out MethodBase method) { foreach (Type t in SelfAndBaseTypes(type)) { #if !(NETFX_CORE || DNXCORE50) MemberInfo[] members = t.GetDefaultMembers(); #else MemberInfo[] members = new MemberInfo[0]; #endif if (members.Length != 0) { IEnumerable<MethodBase> methods = members. OfType<PropertyInfo>(). #if !(NETFX_CORE || DNXCORE50) Select(p => (MethodBase)p.GetGetMethod()). Where(m => m != null); #else Select(p => (MethodBase)p.GetMethod); #endif int count = FindBestMethod(methods, args, out method); if (count != 0) return count; } } method = null; return 0; }
bool EvaluateMethod (string str, MemberInfo[] member, object instance, object [] parameterValues, out object val) { val = null; // Find a method with a matching number of parameters var method = FindBestOverload (member.OfType<MethodBase> (), parameterValues); if (method == null) return false; try { // Convert the given parameters to the types specified in the method signature var methodParams = method.GetParameters (); var convertedArgs = (methodParams.Length == parameterValues.Length) ? parameterValues : new object [methodParams.Length]; int numArgs = methodParams.Length; Type paramsArgType = null; if (methodParams.Length > 0 && methodParams [methodParams.Length - 1].ParameterType.IsArray && methodParams [methodParams.Length - 1].IsDefined (typeof (ParamArrayAttribute))) { paramsArgType = methodParams [methodParams.Length - 1].ParameterType.GetElementType (); numArgs--; } int n; for (n = 0; n < numArgs; n++) convertedArgs [n] = ConvertArg (method, n, parameterValues [n], methodParams [n].ParameterType); if (methodParams.Length == parameterValues.Length && paramsArgType != null) { // Invoking an method with a params argument, but the number of arguments provided is the same as the // number of arguments of the method, so the last argument can be either one of the values of the // params array, or it can be the whole params array. try { var last = convertedArgs.Length - 1; convertedArgs [last] = ConvertArg (method, last, parameterValues [last], methodParams [last].ParameterType); // Conversion worked. Ignore the params argument. paramsArgType = null; } catch (InvalidCastException) { // Conversion of the last argument failed, so it probably needs to be handled as a single value // for the params argument. } } if (paramsArgType != null) { var argsArray = new object [parameterValues.Length - numArgs]; for (int m = 0; m < argsArray.Length; m++) argsArray [m] = ConvertArg (method, n, parameterValues [n++], paramsArgType); convertedArgs [convertedArgs.Length - 1] = argsArray; } // Invoke the method val = method.Invoke (instance, convertedArgs); } catch (Exception ex) { LoggingService.LogError ("MSBuild property evaluation failed: " + str, ex); return false; } return true; }