I am writing a decorating proxy using Castle DynamicProxy. I need the proxy's interceptor to intercept only property writes (not reads) so I am checking the name of the method thusly:
public void Intercept(IInvocation invocation)
{
if (invocation.Method.Name.StartsWith("set_")
{
// ...
}
invocation.Proceed();
}
Now this works fine but I don't like the fact my proxy has intimate knowledge of how properties are implemented: I'd like to replace the method name check with something akin to:
if (invocation.Method.IsPropertySetAccessor)
Unfortunately my Google-fu has failed me. Any ideas?
You could check whether a property exists for which this method is the setter (untested):
bool isSetAccessor = invocation.Method.DeclaringType.GetProperties()
.Any(prop => prop.GetSetMethod() == invocation.Method)
(Inspiration taken from Marc's answer to a related question.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With