/// <summary> /// Returns true if the method's parameter types are reference assignable from /// the argument types, otherwise false. /// /// An example that can make the method return false is that /// typeof(double).GetMethod("op_Equality", ..., new[] { typeof(double), typeof(int) }) /// returns a method with two double parameters, which doesn't match the provided /// argument types. /// </summary> private static bool MatchesArgumentTypes(this MethodInfo mi, Type[] argTypes) { Debug.Assert(argTypes != null); if (mi == null) { return(false); } ParameterInfo[] ps = mi.GetParametersCached(); if (ps.Length != argTypes.Length) { return(false); } for (int i = 0; i < ps.Length; i++) { if (!TypeUtils.AreReferenceAssignable(ps[i].ParameterType, argTypes[i])) { return(false); } } return(true); }
public static Expression ValidateOneArgument(MethodBase method, ExpressionType nodeKind, Expression arg, ParameterInfo pi) { RequiresCanRead(arg, "arguments"); Type pType = pi.ParameterType; if (pType.IsByRef) { pType = pType.GetElementType(); } TypeUtils.ValidateType(pType); if (!TypeUtils.AreReferenceAssignable(pType, arg.Type)) { if (!TryQuote(pType, ref arg)) { // Throw the right error for the node we were given switch (nodeKind) { case ExpressionType.New: throw Error.ExpressionTypeDoesNotMatchConstructorParameter(arg.Type, pType); case ExpressionType.Invoke: throw Error.ExpressionTypeDoesNotMatchParameter(arg.Type, pType); case ExpressionType.Dynamic: case ExpressionType.Call: throw Error.ExpressionTypeDoesNotMatchMethodParameter(arg.Type, pType, method); default: throw ContractUtils.Unreachable; } } } return(arg); }