I would like to use reflection to display a list of methods in an interface.
public interface IRoadVehicle
{
int WheelCount { get; }
bool IsEmergency();
}
I use following code:
foreach (var m in typeof(IRoadVehicle).GetMethods())
{
Console.WriteLine(m.Name);
}
However, I also get listed the compiler-generated property accessors if the interface has a property. I would like to differentiate between explicitly-defined methods and property accessors to omit the latter.
//output:
//get_WheelCount
//IsEmergency
//desired output:
//IsEmergency
How can I filter out the property-related methods?
You can use the
IsSpecialNameproperty:This removes all methods with a name that is treated somehow special by the compiler. The docs say this about it: