Roy Osherove

View Original

Use Reflection to find Methods that implement explicit interfaces

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)
{
}
}