Created
November 5, 2010 02:29
-
-
Save amirci/663559 to your computer and use it in GitHub Desktop.
Interceptor that stores the properties of an interface
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PropertyInterceptor : IInterceptor | |
{ | |
private readonly IDictionary<string, object> _properties = new Dictionary<string, object>(); | |
public void Intercept(IInvocation invocation) | |
{ | |
var key = invocation.Method.Name.Substring(4); | |
if (invocation.Method.Name.StartsWith("set_")) | |
{ | |
_properties[key] = invocation.Arguments[0]; | |
return; | |
} | |
if (invocation.Method.Name.StartsWith("get_")) | |
{ | |
object value; | |
_properties.TryGetValue(key, out value); | |
if (value == null && invocation.TargetType.IsValueType) | |
{ | |
value = Activator.CreateInstance(invocation.TargetType); | |
} | |
invocation.ReturnValue = value; | |
return; | |
} | |
invocation.Proceed(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment