Advertisement
Guest User

Untitled

a guest
Dec 21st, 2014
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. public class GenericInvokerTest
  2. {
  3. [Test]
  4. public void ShouldDynamicallProxyAGenericClass()
  5. {
  6.  
  7. var sut = new Sut();
  8. var injectableBehaviour = Substitute.For<IInjectableBehaviour>();
  9. var wrapped = Wrapper<Sut>.Create(new Sut(), injectableBehaviour);
  10.  
  11. var result = wrapped.AddOne(10);
  12.  
  13. Assert.AreEqual(11, result);
  14. injectableBehaviour.Received().Invoke();
  15. }
  16.  
  17. public class Sut : MarshalByRefObject
  18. {
  19. public int AddOne(int i)
  20. {
  21. return i + 1;
  22. }
  23. }
  24.  
  25. public interface IInjectableBehaviour
  26. {
  27. void Invoke();
  28. }
  29.  
  30.  
  31. public class Wrapper<T> : RealProxy
  32. {
  33. private readonly T _instance;
  34.  
  35. IInjectableBehaviour _behaviour;
  36.  
  37. private Wrapper(T instance, IInjectableBehaviour behaviour)
  38. : base(typeof(T))
  39. {
  40. _behaviour = behaviour;
  41.  
  42. _instance = instance;
  43. }
  44.  
  45. public static T Create(T instance, IInjectableBehaviour behaviour)
  46. {
  47. return (T)new Wrapper<T>(instance, behaviour).GetTransparentProxy();
  48. }
  49.  
  50. public override IMessage Invoke(IMessage msg)
  51. {
  52. var methodCall = (IMethodCallMessage)msg;
  53. var method = (MethodInfo)methodCall.MethodBase;
  54.  
  55. _behaviour.Invoke();
  56. var result = method.Invoke(_instance, methodCall.InArgs);
  57. return new ReturnMessage(result, null, 0, methodCall.LogicalCallContext, methodCall);
  58. }
  59. }
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement