从温莎城堡拦截器访问方法的自定义属性
我正在尝试访问应用于城堡拦截器中的方法的自定义属性,例如:
[MyCustomAttribute(SomeParam = "attributeValue")]
public virtual MyEntity Entity { get; set; }
使用以下代码:
internal class MyInterceptor : IInterceptor
{
public void Intercept(IInvocation invocation)
{
if (invocation.Method.GetCustomAttributes(typeof(MyCustomAttribute), true) != null)
{
//Do something
}
}
}
调用方法时拦截器正常触发,但此代码不返回自定义属性。我怎样才能实现这个目标?
I am trying to access a custom attribute applied to a method within a castle interceptor, e.g.:
[MyCustomAttribute(SomeParam = "attributeValue")]
public virtual MyEntity Entity { get; set; }
using the following code:
internal class MyInterceptor : IInterceptor
{
public void Intercept(IInvocation invocation)
{
if (invocation.Method.GetCustomAttributes(typeof(MyCustomAttribute), true) != null)
{
//Do something
}
}
}
The interceptor is firing OK when the method is called but this code does not return the custom attribute. How can I achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试 Attribute.GetCustomAttribute(...) 静态方法。这很奇怪,但这两种方法有时会由于某些奇怪的原因返回不同的结果。
Try
Attribute.GetCustomAttribute(...)
static method for this. It's bizarre but these two methods return different results sometimes for some strange reason.尝试
Try
我想我已经弄清楚了 - 这是因为属性和方法之间的差异。触发拦截器的是get_方法,而这个并没有用父属性的属性来修饰。
I think I've figured it out - it is because of the difference between the property and the method. It is the get_ method that triggers the interceptor, and this is not decorated with the attribute of the parent property.