A property method has three extra characteristics, compared with a normal method:
- They always start with
get_
or set_
, while a normal method CAN start with those prefixes.
- The property
MethodInfo.IsSpecialName
is set to true.
- The MethodInfo has a custom attribute
System.Runtime.CompilerServices.CompilerGeneratedAttribute
.
You could check on 1, combined with option 2 or 3. Since the prefixes are a standard, you should not really worry about checking on it.
The other method is to enumerate through all properties and match the methods, which will be much slower:
public bool IsGetter(MethodInfo method)
{
if (!method.IsSpecialName)
return false; // Easy and fast way out.
return method.DeclaringType
.GetProperties(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)
.Any(p => p.GetGetMethod() == method);
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…