public void ExceptionInInjectedMethodIsThrownProperly() { MethodInfo method = typeof(ThrowsExceptionFromInjectedMethod).GetMethod("Foo"); IMethodInjector injector = Factory.GetInjector(method); Assert.That(injector, Is.Not.Null); ThrowsExceptionFromInjectedMethod mock = new ThrowsExceptionFromInjectedMethod(); injector.Invoke(mock, new object[0]); }
public void MethodInjectorCanReturnReferenceType() { MethodInfo method = typeof(MethodInvocationObject).GetMethod("Foo"); IMethodInjector injector = Factory.GetInjector(method); Assert.That(injector, Is.Not.Null); MethodInvocationObject mock = new MethodInvocationObject(); string result = (string)injector.Invoke(mock, new object[] { 42 }); Assert.That(result, Is.EqualTo("42")); }
public void MethodInjectorCanReturnValueType() { MethodInfo method = typeof(MethodInvocationObject).GetMethod("Boink"); IMethodInjector injector = Factory.GetInjector(method); Assert.That(injector, Is.Not.Null); MethodInvocationObject mock = new MethodInvocationObject(); int result = (int)injector.Invoke(mock, new object[] { 12 }); Assert.That(result, Is.EqualTo(120)); }
public void MethodInjectorCanCallGenericMethod() { MethodInfo gtd = typeof(ObjectWithGenericMethod).GetMethod("ConvertGeneric"); MethodInfo method = gtd.MakeGenericMethod(typeof(int)); IMethodInjector injector = Factory.GetInjector(method); Assert.That(injector, Is.Not.Null); ObjectWithGenericMethod obj = new ObjectWithGenericMethod(); string result = injector.Invoke(obj, new object[] { 42 }) as string; Assert.That(result, Is.EqualTo("42")); }
/*----------------------------------------------------------------------------------------*/ #region Private Methods private void DeliverMessage(object sender, object args) { try { _injector.Invoke(_subscriber, new object[] { sender, args }); } catch (TargetInvocationException ex) { if (ex.InnerException != null) { throw ex.InnerException; } } }
/*----------------------------------------------------------------------------------------*/ /// <summary> /// Executed when the instance is being initialized. /// </summary> /// <param name="context">The activation context.</param> /// <returns>A value indicating whether to proceed or stop the execution of the strategy chain.</returns> public override StrategyResult Initialize(IContext context) { IList <MethodInjectionDirective> directives = context.Plan.Directives.GetAll <MethodInjectionDirective>(); if (directives.Count > 0) { var injectorFactory = context.Binding.Components.InjectorFactory; foreach (MethodInjectionDirective directive in directives) { // Resolve the arguments that should be injected via the method's arguments. object[] arguments = ResolveArguments(context, directive); // Get an injector that can call the method. IMethodInjector injector = injectorFactory.GetInjector(directive.Member); // Call the method. injector.Invoke(context.Instance, arguments); } } return(StrategyResult.Proceed); }