public void Test() { var proxy = SimpleProxyFactory.CreateProxyWithoutTarget <IOne>(new DelegateHandler(c => c.MethodInfo.Name), typeof(ITwo)); Assert.That(proxy.One(), Is.EqualTo("One")); Assert.That(((ITwo)proxy).Two(), Is.EqualTo("Two")); }
public void Test() { var impl = new Impl(); var proxy = SimpleProxyFactory.CreateProxyForTarget <IInterface>(new DelegateInterceptor(c => c.Proceed()), impl); proxy.Foo(1, 2, 3, "four"); Assert.That(impl.Invocations.Single(), Is.EqualTo(new object[] { 1, 2, 3, "four" })); }
public Benchmark() { manualWithoutTarget = new Impl(); manualForTarget = new Decorator(new Impl()); castleForTarget = new ProxyGenerator().CreateInterfaceProxyWithTarget <IInterface>(new Impl(), new CastleInterceptor(true)); simpleForTarget = SimpleProxyFactory.CreateProxyForTarget <IInterface>(new SimpleInterceptor(), new Impl()); castleWithoutTarget = new ProxyGenerator().CreateInterfaceProxyWithoutTarget <IInterface>(new CastleInterceptor(false)); simpleWithoutTarget = SimpleProxyFactory.CreateProxyWithoutTarget <IInterface>(new SimpleInterceptor()); }
public void Test() { var invocations = new List <MethodInvocation>(); var proxy = SimpleProxyFactory.CreateProxyWithoutTarget <IFoo>(new DelegateHandler(c => invocations.Add(c))); proxy.Foo(1, 2, 3); Assert.That(invocations.Single().MethodInfo.Name, Is.EqualTo("Foo")); Assert.That(invocations.Single().Arguments.Single(), Is.EqualTo(new object[] { 1, 2, 3 })); }
public void Test() { var invocations = new List <MethodInvocation>(); var proxy = SimpleProxyFactory.CreateProxyWithoutTarget <IFoo>(new DelegateHandler(c => invocations.Add(c))); proxy.Foo("one", "two"); Assert.That(invocations.Single().MethodInfo, Is.EqualTo(typeof(IFoo).GetMethod("Foo"))); Assert.That(invocations.Single().Arguments, Is.EqualTo(new object[] { "one", "two" })); }
public void Test() { var proxy = SimpleProxyFactory.CreateProxyForTarget <ITwo>(new DelegateInterceptor(args => { args.Proceed(); }), new Impl(), typeof(IOne)); Assert.That(proxy.Two(), Is.EqualTo("two")); Assert.That(((IOne)proxy).One(), Is.EqualTo("one")); }
public void ReturnValueOfWrongType_ThrowException() { var target = new Impl(); var proxy = SimpleProxyFactory.CreateProxyForTarget <IInterface>(new DelegateInterceptor(args => { args.Proceed(); args.Result = new object(); }), target); Assert.Throws <InvalidCastException>(() => proxy.Foo()); }
public void Test() { var withoutTarget1 = SimpleProxyFactory.CreateProxyWithoutTarget <IInterface>(new DelegateHandler(_ => "withoutTarget1")); var withoutTarget2 = SimpleProxyFactory.CreateProxyWithoutTarget <IInterface>(new DelegateHandler(_ => "withoutTarget2")); var withTarget1 = SimpleProxyFactory.CreateProxyForTarget <IInterface>(new DelegateInterceptor(args => args.Result = "newValue1"), new Impl("originalValue1")); var withTarget2 = SimpleProxyFactory.CreateProxyForTarget <IInterface>(new DelegateInterceptor(args => args.Result = "newValue2"), new Impl("originalValue2")); Assert.That(withoutTarget1.Foo(), Is.EqualTo("withoutTarget1")); Assert.That(withoutTarget2.Foo(), Is.EqualTo("withoutTarget2")); Assert.That(withTarget1.Foo(), Is.EqualTo("newValue1")); Assert.That(withTarget2.Foo(), Is.EqualTo("newValue2")); }
public void CanAlterReturnValue() { var target = new Impl(); var proxy = SimpleProxyFactory.CreateProxyForTarget <IInterface>(new DelegateInterceptor(args => { args.Proceed(); args.Result = "43"; }), target); var actual = proxy.Foo(); Assert.That(actual, Is.EqualTo("43")); }
public void Test() { var target = new Impl(); var proxy = SimpleProxyFactory.CreateProxyForTarget <IInterface>(new DelegateInterceptor(args => { args.Invocation.Arguments[0] = 43; args.Proceed(); }), target); proxy.Foo(42); Assert.That(target.Invocations.Single(), Is.EqualTo(43)); }
public void Test() { var target = new Impl(); var proxy = SimpleProxyFactory.CreateProxyForTarget <IInterface>(new DelegateInterceptor(args => { Console.WriteLine("invocation"); args.Proceed(); }), target); proxy.Foo(); Assert.That(target.Invocations, Is.EqualTo(1)); }
public void CreateProxyForClosedInterface() { var intProxy = SimpleProxyFactory.CreateProxyForTarget <IGenericInterface <int> >(new DelegateInterceptor(args => { args.Invocation.Arguments[0] = (int)args.Invocation.Arguments[0] + 1; args.Proceed(); args.Result = (int)args.Result + 1; }), new Impl <int>()); var stringProxy = SimpleProxyFactory.CreateProxyForTarget <IGenericInterface <string> >(new DelegateInterceptor(args => args.Proceed()), new Impl <string>()); Assert.That(intProxy.Foo(42), Is.EqualTo(44)); Assert.That(stringProxy.Foo("foo"), Is.EqualTo("foo")); }
public void CanInspectReturnValue() { var target = new Impl(); string returnValue = ""; var proxy = SimpleProxyFactory.CreateProxyForTarget <IInterface>(new DelegateInterceptor(args => { args.Proceed(); returnValue = (string)args.Result; }), target); var actual = proxy.Foo(); Assert.That(returnValue, Is.EqualTo("42")); Assert.That(actual, Is.EqualTo("42")); }
public void Test() { var proxy = SimpleProxyFactory.CreateProxyForTarget <IInterface>(new DelegateInterceptor(a => { a.Invocation.Arguments[0] = 123; a.Proceed(); }), new Impl()); Assert.Throws <InvalidCastException>(() => proxy.Foo("one", 2)); proxy = SimpleProxyFactory.CreateProxyForTarget <IInterface>(new DelegateInterceptor(a => { a.Invocation.Arguments[1] = "123"; a.Proceed(); }), new Impl()); Assert.Throws <InvalidCastException>(() => proxy.Foo("one", 2)); }
public void Test() { var proxy = SimpleProxyFactory.CreateProxyWithoutTarget <IInterface>(new DelegateHandler(c => "42")); Assert.That(proxy.Foo(), Is.EqualTo("42")); }