从的getter / setter的MethodInfo的寻找托管的PropertyInfo [英] Finding the hosting PropertyInfo from the MethodInfo of getter/setter

查看:129
本文介绍了从的getter / setter的MethodInfo的寻找托管的PropertyInfo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用反射做一些类型的分析在运行。如果我有一个MethodInfo的实例, 我怎么能弄清楚,如果这是一个真正的方法,或者是财产的一个getter / setter方法​​?如果它是一个属性,我怎么能找到其托管的PropertyInfo回来吗?

I do some type analysis in runtime using Reflection. If I have a MethodInfo instance, how can I figure out if this is a "real" method or is a getter/setter method of a property? And if it is a property, how can I find the its hosting PropertyInfo back?

推荐答案

的Ecma 335指定(但不要求),该编译器使用的get_ / SET_ prefixes(章节22.28)。我不知道,打破这一建议的任何语言​​。因此很容易:

Ecma 335 specifies (but does not demand) that compilers use the get_/set_ prefixes (chapter 22.28). I don't know any language that breaks that recommendation. Making it easy:

public static PropertyInfo GetPropFromMethod(Type t, MethodInfo method) {
  if (!method.IsSpecialName) return null;
  return t.GetProperty(method.Name.Substring(4), 
    BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic);
}

这篇关于从的getter / setter的MethodInfo的寻找托管的PropertyInfo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆