Use Reflection to find Methods that implement explicit interfaces

By Roy Osherove In .NET

Markdown version

Historical article · Originally published October 15, 2008; technical details may be outdated.

update: added a check for IsPrivate based on the comments.

so I won't forget: here's how you can iterate over a type definition's methods and see if one of them is an explicit intreface definition:

foreach (var info in GetType().GetMethods(
         BindingFlags.NonPublic|BindingFlags.Instance|BindingFlags.DeclaredOnly))
  {
   if (info.IsFinal && info.IsPrivate)
   {
         Console.WriteLine("Explicit interface implementation: {0}", info.Name);
   }
  }

this will show only the "Write" method for the following class:

public class Class1 : ILogger
{
public void show()
{
} 
void MyMethod()
{
}
void ILogger.Write(string s)
{
}
}

More in .NET

All posts in .NET