Skip to content

Instantly share code, notes, and snippets.

@wislon
Last active December 19, 2015 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wislon/5972699 to your computer and use it in GitHub Desktop.
Save wislon/5972699 to your computer and use it in GitHub Desktop.
Quick and dirty using System.Reflection to get method names from class/type in assembly
// For better way which actually allows you to retrieve the method body and parse it, have a look at Mono.Cecil
// and links below:
// see http://stackoverflow.com/questions/5741350/look-if-a-method-is-called-inside-a-method-using-reflection
// and http://stackoverflow.com/questions/4372205/how-to-inject-call-to-system-object-equals-with-mono-cecil
// and http://www.codeproject.com/Articles/499960/Accessing-Assembly-Metadata-with-Reflection-or-Mon
private static IEnumerable<MethodInfo> GetTheMethods()
{
string assemblyFile = Assembly.GetAssembly(typeof (YourClass)).Location;
Assert.IsTrue(File.Exists(assemblyFile));
var allModules = Assembly.LoadFrom(assemblyFile).GetModules().Select(modules => modules).ToList();
Assert.IsTrue(allServiceModules.Any());
var theModule = allModules.First();
var yourType = theModule.GetTypes().First(t => t.Name.Equals("yourClassName"));
Assert.IsNotNull(yourType);
// method names to exclude, just for example
var methodExceptions = new SortedSet<string> {"Dispose", "ToString", "Equals", "GetHashCode", "GetType"};
var methods = yourType.GetMethods()
.Except(yourType.GetMethods()
.Where(m => m.Name.StartsWith("get_")
|| m.Name.StartsWith("set_")
|| methodExceptions.Contains(m.Name)))
.ToList();
return methods;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment