确定 MethodInfo 实例是否是属性访问器

发布于 2024-12-10 11:30:36 字数 492 浏览 0 评论 0 原文

我正在使用 Castle DynamicProxy 编写装饰代理。我需要代理的拦截器仅拦截属性写入(而不是读取),因此我这样检查方法的名称:

public void Intercept(IInvocation invocation)
{
    if (invocation.Method.Name.StartsWith("set_")
    {
        // ...
    }

    invocation.Proceed();
}

现在这工作正常,但我不喜欢我的代理对属性的实现方式有深入的了解:我'我想用类似的东西替换方法名称检查:

if (invocation.Method.IsPropertySetAccessor)

不幸的是我的Google-fu让我失败了。有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

痴情换悲伤 2024-12-17 11:30:37

我想你可以尝试使用扩展方法:
http://msdn.microsoft.com/en-us/library/bb383977.aspx

I think you may try using extension methods:
http://msdn.microsoft.com/en-us/library/bb383977.aspx

缘字诀 2024-12-17 11:30:36

您可以检查是否存在此方法作为设置器的属性(未经测试):(

bool isSetAccessor = invocation.Method.DeclaringType.GetProperties() 
        .Any(prop => prop.GetSetMethod() == invocation.Method)

灵感来自 马克对相关问题的回答。)

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.)

难忘№最初的完美 2024-12-17 11:30:36

据我所知,没有任何巫术。您可以,也许,剥离set_,查找具有该名称的属性,然后比较MethodInfo实例(inplication.Method )到属性访问器(GetSetMethod()) - 但是,我不能诚实地说(不检查)您是否会获得相同的 MethodInfo 实例(即使相同 方法)。

if(method.IsSpecialName && method.Name.StartsWith("set_"))
{
    var prop = typeof (Foo).GetProperty(method.Name.Substring(4),
           BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
    var accessor = prop.GetSetMethod();
    bool isSame = accessor == method;
}

There isn't any voodoo of which I'm aware. You could, perhaps, strip the set_, look for a property with that name, and compare the MethodInfo instance (invocation.Method) to the property accessor (GetSetMethod()) - however, I can't honestly say (without checking) whether you will get the same MethodInfo instance (even if it is the same method).

if(method.IsSpecialName && method.Name.StartsWith("set_"))
{
    var prop = typeof (Foo).GetProperty(method.Name.Substring(4),
           BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
    var accessor = prop.GetSetMethod();
    bool isSame = accessor == method;
}
只是偏爱你 2024-12-17 11:30:36

我不确定 incall.Method 是什么类型,但如果您可以获得 PropertyInfo,则可以使用 IsSpecialName。不幸的是,这不仅表明该属性是 set_ 还是 _get,而且还表明它是否是重载运算符。

I'm not sure what kind of type the invocation.Method is, but if you can get the PropertyInfo you can use the IsSpecialName. Unfortunately this tells not only if the property is a set_ or _get but also if it is an overloaded operator.

蓝色星空 2024-12-17 11:30:36

首先,您可以检查 MethodInfo 类的 MemberType 属性,看看它是否是一个 Property

现在,您应该尝试猜测它是 get 还是 set。如果您不想分析名称(有人可能将方法命名为“set_Something”),那么您可以检查参数。

  • 如果属性访问器接受一个参数并返回 void
    它是一个集合
  • 如果属性访问器返回一个值并且不选择参数,它是一个获取

您可能只对第一个检查感兴趣

First, you can examine the MemberType property of MethodInfo class to see if it's a Property.

Now, you should try to guess if it's a get or a set. If you don't want to analyse the name (someone might name a method "set_Something"), then you can check the arguments.

  • If the property accessor accepts one parameter and returns void,
    it is a set
  • If the property accessor returns one value and doesn't pick parameters, it is a get

You may be interested only in the first check

鸠书 2024-12-17 11:30:36

从您的 MethodInfo 对象获取 MemberType 属性应该说这是一个 属性类型 因此您应该能够将其转换为 PropertyInfo 代替。该对象公开属性 CanWrite ,该属性告诉这是一个二传手。

from your MethodInfo object get the MemberType property which should say this is a Property type so you should be able to cast it to PropertyInfo instead. that object exposes the property CanWrite which tells if this is a setter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文