从 getter/setter 的 MethodInfo 中查找托管 PropertyInfo [英] Finding the hosting PropertyInfo from the MethodInfo of getter/setter
本文介绍了从 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_ 前缀(第 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屋!
查看全文