本文介绍了确定一个MethodInfo的实例是一个属性访问器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我写使用一个装饰代理。我需要代理服务器的拦截器拦截唯一的财产写入(未读),所以我正是如此检查方法的名称:
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)
(从的。)
这篇关于确定一个MethodInfo的实例是一个属性访问器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!