Last active
December 19, 2015 14:58
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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