public void ChangeValueInAfterCall() { var impl = new SomeInterfaceImplementation(); var i = new RecordingInterceptor(); var h = new InterceptingAdapterEmittedTypeHanlder(impl, i); var o = _classEmitter.EmitInterfaceInstance <ISomeInterface>(h); i.AfterCallAction = delegate(CallInfo ci) { ci.ReturnValue = "AnotherValue"; }; var result = o.SomeMethod("SomeString", 17); // Adapted method should have been called Assert.IsNotNull(impl.SomeMethodS); Assert.AreEqual("SomeString", impl.SomeMethodS); Assert.AreEqual(17, impl.SomeMethodI); // Both BeforeCall and AfterCall should have been called Assert.IsNotNull(i.BeforeCallInfo); Assert.IsNotNull(i.AfterCallInfo); // Result should be the value set by interceptor Assert.AreEqual("SomeValue", i.AfterCallInfo.ReturnValue); Assert.AreEqual("AnotherValue", result); }
public static void Main() { var example = new SomeInterfaceImplementation() { Value = "A value", } as ISomeInterface; Console.WriteLine($"{example.SomeClass.Key} has value '{example.Value}'"); }
public void AdaptedAndInterceptor() { var impl = new SomeInterfaceImplementation(); var i = new RecordingInterceptor(); var h = new InterceptingAdapterEmittedTypeHanlder(impl, i); var o = _classEmitter.EmitInterfaceInstance <ISomeInterface>(h); var result = o.SomeMethod("StringParam", 17); Assert.IsNotNull(i.BeforeCallInfo); Assert.IsNotNull(i.AfterCallInfo); Assert.AreEqual("StringParam", impl.SomeMethodS); Assert.AreEqual(17, impl.SomeMethodI); Assert.AreEqual("SomeValue", result); // // BeforeCall Assert.AreEqual(2, i.BeforeCallInfo.Arguments.Length); Assert.AreEqual("StringParam", i.BeforeCallInfo.Arguments[0]); Assert.AreEqual(17, i.BeforeCallInfo.Arguments[1]); Assert.AreEqual(2, i.BeforeCallInfo.ArgumentTypes.Length); Assert.AreEqual(typeof(string), i.BeforeCallInfo.ArgumentTypes[0]); Assert.AreEqual(typeof(int), i.BeforeCallInfo.ArgumentTypes[1]); Assert.AreEqual(typeof(ISomeInterface), i.BeforeCallInfo.MethodOwner); Assert.AreEqual("SomeMethod", i.BeforeCallInfo.MethodName); Assert.AreEqual(typeof(string), i.BeforeCallInfo.ResultType); Assert.IsNull(i.BeforeCallInfo.ReturnValue); Assert.IsNull(i.BeforeCallInfo.ThrownException); Assert.IsFalse(i.BeforeCallInfo.Completed); // // AfterCall Assert.AreEqual(2, i.AfterCallInfo.Arguments.Length); Assert.AreEqual("StringParam", i.AfterCallInfo.Arguments[0]); Assert.AreEqual(17, i.AfterCallInfo.Arguments[1]); Assert.AreEqual(2, i.AfterCallInfo.ArgumentTypes.Length); Assert.AreEqual(typeof(string), i.AfterCallInfo.ArgumentTypes[0]); Assert.AreEqual(typeof(int), i.AfterCallInfo.ArgumentTypes[1]); Assert.AreEqual(typeof(ISomeInterface), i.AfterCallInfo.MethodOwner); Assert.AreEqual("SomeMethod", i.AfterCallInfo.MethodName); Assert.AreEqual(typeof(string), i.AfterCallInfo.ResultType); Assert.AreEqual("SomeValue", i.AfterCallInfo.ReturnValue); Assert.IsNull(i.AfterCallInfo.ThrownException); Assert.IsTrue(i.AfterCallInfo.Completed); }
public void AdaptedOnly() { var impl = new SomeInterfaceImplementation(); var h = new InterceptingAdapterEmittedTypeHanlder(impl); var o = _classEmitter.EmitInterfaceInstance <ISomeInterface>(h); var result = o.SomeMethod("StringParam", 17); Assert.AreEqual("StringParam", impl.SomeMethodS); Assert.AreEqual(17, impl.SomeMethodI); Assert.AreEqual("SomeValue", result); }
public void SetThrownExceptionInAfterCall() { var impl = new SomeInterfaceImplementation(); var i = new RecordingInterceptor(); var h = new InterceptingAdapterEmittedTypeHanlder(impl, i); var o = _classEmitter.EmitInterfaceInstance <ISomeInterface>(h); i.AfterCallAction = delegate(CallInfo ci) { ci.ThrownException = new NullReferenceException(); }; bool exceptionBubbledUp = false; try { o.SomeMethod("SomeString", 17); } catch (Exception e) { exceptionBubbledUp = true; Assert.IsInstanceOfType(e, typeof(AdaptedException)); Assert.IsNotNull(e.InnerException); Assert.IsNotNull(((AdaptedException)e).Arguments); Assert.AreEqual("SomeMethod", ((AdaptedException)e).MethodName); Assert.AreEqual(2, ((AdaptedException)e).Arguments.Length); Assert.AreEqual("SomeString", ((AdaptedException)e).Arguments[0]); Assert.AreEqual(17, ((AdaptedException)e).Arguments[1]); Assert.IsFalse(((AdaptedException)e).BeforeCall); Assert.IsFalse(((AdaptedException)e).DuringCall); Assert.IsTrue(((AdaptedException)e).AfterCall); Assert.IsInstanceOfType(e.InnerException, typeof(NullReferenceException)); Assert.IsNull(e.InnerException.InnerException); } Assert.IsTrue(exceptionBubbledUp); Assert.IsNotNull(impl.SomeMethodS); Assert.AreEqual("SomeString", impl.SomeMethodS); Assert.AreEqual(17, impl.SomeMethodI); Assert.IsNotNull(i.BeforeCallInfo); Assert.IsNotNull(i.AfterCallInfo); Assert.IsNull(i.BeforeCallInfo.ThrownException); Assert.IsNull(i.AfterCallInfo.ThrownException); }
public void ExceptionInAdaptedCall() { var impl = new SomeInterfaceImplementation(); var i = new RecordingInterceptor(); var h = new InterceptingAdapterEmittedTypeHanlder(impl, i); var o = _classEmitter.EmitInterfaceInstance <ISomeInterface>(h); bool exceptionBubbledUp = false; try { o.ThrowException(); } catch (Exception e) { exceptionBubbledUp = true; Assert.IsInstanceOfType(e, typeof(AdaptedException)); Assert.IsNotNull(e.InnerException); Assert.IsNotNull(((AdaptedException)e).Arguments); Assert.AreEqual("ThrowException", ((AdaptedException)e).MethodName); Assert.AreEqual(0, ((AdaptedException)e).Arguments.Length); Assert.IsFalse(((AdaptedException)e).BeforeCall); Assert.IsTrue(((AdaptedException)e).DuringCall); Assert.IsFalse(((AdaptedException)e).AfterCall); Assert.IsInstanceOfType(e.InnerException, typeof(NullReferenceException)); Assert.IsNull(e.InnerException.InnerException); } Assert.IsTrue(exceptionBubbledUp); Assert.IsNotNull(i.BeforeCallInfo); Assert.IsNotNull(i.AfterCallInfo); Assert.IsNull(i.BeforeCallInfo.ThrownException); Assert.IsNotNull(i.AfterCallInfo.ThrownException); Assert.IsInstanceOfType(i.AfterCallInfo.ThrownException, typeof(NullReferenceException)); }
public void DoSomeWorkOnImplementation(SomeInterfaceImplementation obj) { obj.SomeMethod(); }