protected IBinding BindToMethods(object target, Type[] genericParameters, object[] arguments, IEnumerable <MethodInfo> methods) { for (int i = 0; i < ParameterStrategies.Count; i++) { ComposeParameterStrategy strategy = ParameterStrategies[i]; foreach (MethodInfo method in methods) { MethodInfo actualMethod = method; if (method.IsGenericMethod) { if (genericParameters == null || method.GetGenericArguments().Length != genericParameters.Length) { continue; } actualMethod = method.MakeGenericMethod(genericParameters); } ParameterInfo[] parameterInfos = actualMethod.GetParameters(); object[] convertedArguments = ComposeParameters(arguments, parameterInfos, strategy.Predicate, strategy.Converter); if (convertedArguments != null) { return(new MethodBinding(actualMethod, target, convertedArguments)); } } } return(null); }
/// <summary> /// Binds to method of a given object including interface methods /// </summary> /// <param name="target">instance on an object</param> /// <param name="methodSelector">Predicate to select methods</param> /// <param name="genericParameters">for generic methods should specify types other vise empty array of type</param> /// <param name="arguments">arguments for method</param> /// <returns>MethodBind or null if binding is not possible</returns> protected IObjectBind BindToMethod(object target, Func <MethodInfo, bool> methodSelector, Type[] genericParameters, object[] arguments) { Type targetType = target as Type; bool processInterface = false; if (targetType == null) { targetType = target.GetType(); processInterface = true; } IEnumerable <MethodInfo> methods = targetType .GetMethods(MethodFilter) .Where(methodSelector); //Process interfaces only if target is not a Type if (processInterface) { foreach (Type interfaceType in targetType.GetInterfaces()) { methods = methods.Concat(interfaceType.GetMethods(MethodFilter).Where(methodSelector)); } } for (int i = 0; i < parameterStrategies.Count; i++) { ComposeParameterStrategy strategy = parameterStrategies[i]; foreach (MethodInfo method in methods) { MethodInfo actualMethod = method; if (method.IsGenericMethod) { actualMethod = method.MakeGenericMethod(genericParameters); } ParameterInfo[] parameterInfos = actualMethod.GetParameters(); object[] convertedArguments = ComposeParameters(arguments, parameterInfos, strategy.Predicate, strategy.Converter); if (convertedArguments != null) { return(new MethodBind(actualMethod, target, convertedArguments)); } } } return(null); }
/// <summary> /// Binds to method /// </summary> /// <param name="target">instance on an object</param> /// <param name="method">specific method</param> /// <param name="arguments">arguments for method</param> /// <returns>MethodBind or null if binding is not possible</returns> public IObjectBind BindToMethod(object target, MethodInfo method, object[] arguments) { for (int i = 0; i < parameterStrategies.Count; i++) { ComposeParameterStrategy strategy = parameterStrategies[i]; ParameterInfo[] parameterInfos = method.GetParameters(); object[] convertedArguments = ComposeParameters(arguments, parameterInfos, strategy.Predicate, strategy.Converter); if (convertedArguments != null) { return(new MethodBind(method, target, convertedArguments)); } } return(null); }
/// <summary> /// Binds to constructor /// </summary> /// <param name="targetType">Type</param> /// <param name="arguments">Arguments for constructor</param> /// <returns>ConstructorBind or null</returns> public IObjectBind BindToConstructor(Type targetType, object[] arguments) { //Use Default Constructor if (arguments == null || arguments.Length == 0) { ConstructorInfo defaultConstructor = targetType.GetConstructor(ConstructorFilter, null, EmptyTypes, null); if (defaultConstructor == null || !CanBind(defaultConstructor)) { return(null); } //return new ConstructorBind(target, null, null); return(new ConstructorBind(defaultConstructor, null)); } IEnumerable <ConstructorInfo> constructors = targetType .GetConstructors(ConstructorFilter) .Where(c => CanBind(c)); for (int i = 0; i < parameterStrategies.Count; i++) { ComposeParameterStrategy strategy = parameterStrategies[i]; foreach (ConstructorInfo constructor in constructors) { ParameterInfo[] parameterInfos = constructor.GetParameters(); object[] convertedArguments = ComposeParameters(arguments, parameterInfos, strategy.Predicate, strategy.Converter); if (convertedArguments != null) { return(new ConstructorBind(constructor, convertedArguments)); } } } return(null); //return new ConstructorBind(target, null, null); }