Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class GenericInvokerTest
- {
- [Test]
- public void ShouldDynamicallProxyAGenericClass()
- {
- var sut = new Sut();
- var injectableBehaviour = Substitute.For<IInjectableBehaviour>();
- var wrapped = Wrapper<Sut>.Create(new Sut(), injectableBehaviour);
- var result = wrapped.AddOne(10);
- Assert.AreEqual(11, result);
- injectableBehaviour.Received().Invoke();
- }
- public class Sut : MarshalByRefObject
- {
- public int AddOne(int i)
- {
- return i + 1;
- }
- }
- public interface IInjectableBehaviour
- {
- void Invoke();
- }
- public class Wrapper<T> : RealProxy
- {
- private readonly T _instance;
- IInjectableBehaviour _behaviour;
- private Wrapper(T instance, IInjectableBehaviour behaviour)
- : base(typeof(T))
- {
- _behaviour = behaviour;
- _instance = instance;
- }
- public static T Create(T instance, IInjectableBehaviour behaviour)
- {
- return (T)new Wrapper<T>(instance, behaviour).GetTransparentProxy();
- }
- public override IMessage Invoke(IMessage msg)
- {
- var methodCall = (IMethodCallMessage)msg;
- var method = (MethodInfo)methodCall.MethodBase;
- _behaviour.Invoke();
- var result = method.Invoke(_instance, methodCall.InArgs);
- return new ReturnMessage(result, null, 0, methodCall.LogicalCallContext, methodCall);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement