Guest User

Untitled

a guest
Jun 20th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. public class PropertyInterceptor : IInterceptor
  2. {
  3. private readonly IDictionary<string, object> _properties = new Dictionary<string, object>();
  4.  
  5. public void Intercept(IInvocation invocation)
  6. {
  7. var key = invocation.Method.Name.Substring(4);
  8.  
  9. if (invocation.Method.Name.StartsWith("set_"))
  10. {
  11. _properties[key] = invocation.Arguments[0];
  12.  
  13. return;
  14. }
  15.  
  16. if (invocation.Method.Name.StartsWith("get_"))
  17. {
  18. object value;
  19.  
  20. _properties.TryGetValue(key, out value);
  21.  
  22. if (value == null && invocation.TargetType.IsValueType)
  23. {
  24. value = Activator.CreateInstance(invocation.TargetType);
  25. }
  26.  
  27. invocation.ReturnValue = value;
  28. return;
  29. }
  30.  
  31. invocation.Proceed();
  32. }
  33. }
Add Comment
Please, Sign In to add comment