Guest User

Untitled

a guest
Feb 21st, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Runtime.Remoting.Proxies;
  7. using System.Runtime.Remoting.Messaging;
  8. using System.Reflection;
  9. using System.Threading;
  10.  
  11. namespace ProxyWithAOP
  12. {
  13. public class AuthenticationProxy<T> : RealProxy
  14. {
  15. private readonly T _decorated;
  16. public AuthenticationProxy(T decorated)
  17. : base(typeof(T))
  18. {
  19. _decorated = decorated;
  20. }
  21.  
  22. private void Log(string msg, object arg = null)
  23. {
  24. Console.ForegroundColor = ConsoleColor.Green;
  25. Console.WriteLine(msg, arg);
  26. Console.ResetColor();
  27. }
  28.  
  29. public override IMessage Invoke(IMessage msg)
  30. {
  31. var methodCall = msg as IMethodCallMessage;
  32. var methodInfo = methodCall.MethodBase as MethodInfo;
  33.  
  34. if (Thread.CurrentPrincipal.IsInRole("ADMIN"))
  35. {
  36. try
  37. {
  38. Log("User authenticated - You can execute '{0}' ", methodCall.MethodName);
  39.  
  40. var result = methodInfo.Invoke(_decorated, methodCall.InArgs);
  41.  
  42. return new ReturnMessage(result, null, 0, methodCall.LogicalCallContext, methodCall);
  43. }
  44. catch (Exception e)
  45. {
  46. Log(string.Format("User authenticated - Exception {0} executing '{1}'", e), methodCall.MethodName);
  47.  
  48. return new ReturnMessage(e, methodCall);
  49. }
  50. }
  51.  
  52. Log("User not authenticated - You can't execute '{0}' ", methodCall.MethodName);
  53.  
  54. return new ReturnMessage(-1, null, 0, methodCall.LogicalCallContext, methodCall);
  55. }
  56. }
  57. }
  58.  
  59. using System;
  60. using System.Collections.Generic;
  61. using System.Linq;
  62. using System.Runtime.Remoting.Proxies;
  63. using System.Runtime.Remoting.Messaging;
  64. using System.Reflection;
  65. using System.Text;
  66. using System.Threading.Tasks;
  67.  
  68. namespace ProxyWithAOP
  69. {
  70. class DynamicProxy<T> : RealProxy
  71. {
  72. private readonly T _decorated;
  73.  
  74. public DynamicProxy(T decorated)
  75. : base(typeof(T))
  76. {
  77. _decorated = decorated;
  78. }
  79.  
  80. private void Log(string msg, object arg = null)
  81. {
  82. Console.ForegroundColor = ConsoleColor.Red;
  83. Console.WriteLine(msg, arg);
  84. Console.ResetColor();
  85. }
  86.  
  87. public override IMessage Invoke(IMessage msg)
  88. {
  89. var methodCall = msg as IMethodCallMessage;
  90. var methodInfo = methodCall.MethodBase as MethodInfo;
  91.  
  92. Log("In Dynamic Proxy - Before executing '{0}'", methodCall.MethodName);
  93.  
  94. try
  95. {
  96. var result = methodInfo.Invoke(_decorated, methodCall.InArgs);
  97.  
  98. Log("In Dynamic Proxy - After executing '{0}' ", methodCall.MethodName);
  99.  
  100. return new ReturnMessage(result, null, 0, methodCall.LogicalCallContext, methodCall);
  101. }
  102. catch (Exception e)
  103. {
  104. Log(string.Format("In Dynamic Proxy- Exception {0} executing '{1}'", e), methodCall.MethodName);
  105.  
  106. return new ReturnMessage(e, methodCall);
  107. }
  108. }
  109. }
  110. }
  111.  
  112. var decorated = TraceProxy<ICustomerOperations>.Create(new CustomerOperations());
  113. decorated = AuthenticationProxy<ICustomerOperations>.Create(decorated);
  114.  
  115. decorated.GetCustomer(1);
  116. decorated.GetBalance(1);
  117.  
  118. decorated.GetCustomer(1); call has been successful as return type match as `int`. However `decorated.GetBalance(1);` is throwing `InvalidCastException` exception as its excepting `double` return type value.
Add Comment
Please, Sign In to add comment