Is the method naming for property getters/setters standardized in IL?
Asked Answered
U

2

10

I have the following two methods that I am wondering if they are appropriate:

public bool IsGetter(MethodInfo method)
{
    return method.IsSpecialName
        && method.Name.StartsWith("get_", StringComparison.Ordinal);
}

public bool IsSetter(MethodInfo method)
{
    return method.IsSpecialName
        && method.Name.StartsWith("set_", StringComparison.Ordinal);
}

While this code works, I'm hoping to avoid the portion that checks the StartsWith and programmatically get the naming convention. Basically, are there any .NET 4.5 classes that are able to see if the MethodInfo is a property getter/setter?

Ursel answered 23/5, 2013 at 16:5 Comment(1)
While I too would avoid the magic strings, they are in fact standardized.Anabelle
H
16

A property method has three extra characteristics, compared with a normal method:

  1. They always start with get_ or set_, while a normal method CAN start with those prefixes.
  2. The property MethodInfo.IsSpecialName is set to true.
  3. 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);
}
Hyperform answered 23/5, 2013 at 16:25 Comment(2)
Excellent answer - I was popping by to add something similar, but you have it covered.Brahmaputra
Just an addition to this, if a property comes from an interface and is implemented explicitly its name becomes the name of the class + .get_ (or ._set) + the property name. I was looking for ICollection<T>.IsReadOnly and in the debugger it shows its name as System.Collections.Generic.ICollection<T>.get_IsReadOnly.Weinstein
N
1

You could try the following:

public bool IsGetter(MethodInfo method)
{
    return method.DeclaringType.GetProperties().
                                Any(propInfo => propInfo.GetMethod == method);
}

You can optionally specify the binding flags for GetProperties

Negro answered 23/5, 2013 at 16:12 Comment(6)
Now, it only checks for instance and public properties.Hyperform
@MartinMulder, sorry, I did not understand your comment?Negro
@vc 74: If the property, to which the method belongs, would be a private or static property, your code would fail.Hyperform
@MartinMulder, yes, hence my comment at the bottom of the postNegro
@vc 74: Sorry, did not read that. On the other hand: why not query ALL properties? why this obvious ommission?Hyperform
@MartinMulder I'm sure the OP is able to find the combination that works best for him on his own, by looking at the page in MSDN instead of copy/pasting my answer, he may learn something :)Negro

© 2022 - 2024 — McMap. All rights reserved.