Skip to content

Instantly share code, notes, and snippets.

@amirci
Created November 5, 2010 02:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amirci/663559 to your computer and use it in GitHub Desktop.
Save amirci/663559 to your computer and use it in GitHub Desktop.
Interceptor that stores the properties of an interface
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