internal static PropertyInfo ResolveProperty(Type type, string name, bool ignoreCase, object[] indexArgs, bool hasInstance, object setterValue = null, bool getter = true) { var candidates = type.GetAllProperties().Where(prop => MockingUtil.StringEqual(prop.Name, name, ignoreCase)).ToArray(); if (candidates.Length == 1) { return(candidates[0]); } if (!getter) { Array.Resize(ref indexArgs, indexArgs.Length + 1); indexArgs[indexArgs.Length - 1] = setterValue; } var propMethods = candidates .Select(prop => getter ? prop.GetGetMethod(true) : prop.GetSetMethod(true)) .Where(m => m != null && CanCall(m, hasInstance)) .ToArray(); indexArgs = indexArgs ?? MockingUtil.NoObjects; object state; var foundGetter = MockingUtil.BindToMethod(MockingUtil.AllMembers, propMethods, ref indexArgs, null, null, null, out state); return(candidates.First(prop => (getter ? prop.GetGetMethod(true) : prop.GetSetMethod(true)) == foundGetter)); }
public override DynamicMetaObject BindInvokeMember(InvokeMemberBinder binder, DynamicMetaObject[] args) { var wrapper = this.Value as ExpressionContainer; var valueExpr = wrapper.Expression; var typeArgs = MockingUtil.TryGetTypeArgumentsFromBinder(binder); var candidateMethods = valueExpr.Type.GetAllMethods() .Where(m => MockingUtil.StringEqual(m.Name, binder.Name, binder.IgnoreCase) && m.IsStatic == wrapper.IsStatic) .Where(m => m.GetParameters().Length >= args.Length) .Select(m => { if (typeArgs == null) { return(MockingUtil.TrySpecializeGenericMethod(m, args.Select(a => a.RuntimeType).ToArray()) ?? m); } else { return(MockingUtil.TryApplyTypeArguments(m, typeArgs)); } }) .Where(m => m != null) .Where(m => { var methodParams = m.GetParameters(); for (int i = 0; i < args.Length; ++i) { var matcher = UnwrapMatcher(args[i]); if (matcher != null) { var argType = matcher.ReturnType; if (!methodParams[i].ParameterType.IsAssignableFrom(argType)) { return(false); } } } return(true); }) .ToArray(); if (candidateMethods.Length == 0 && args.Length == 0) { return(DoBindGetMember(binder.ReturnType, binder.Name, binder.IgnoreCase)); } var methodArgs = args.Select(a => { var matcher = UnwrapMatcher(a); return(matcher != null ? matcher.ReturnType.GetDefaultValue() : a.Value); }).ToArray(); object state; var method = (MethodInfo)MockingUtil.BindToMethod(MockingUtil.Default, candidateMethods, ref methodArgs, null, null, null, out state); var memberExpr = Expression.Call(!wrapper.IsStatic ? valueExpr : null, method, args.Select(FromArg).ToArray()); return(CreateRecorder(memberExpr, binder.ReturnType, BindingRestrictions.GetInstanceRestriction(this.Expression, this.Value))); }