Created
August 21, 2013 03:09
-
-
Save distantcam/6289949 to your computer and use it in GitHub Desktop.
Mono.Cecil Extension Methods
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
public static class CecilExtensions | |
{ | |
/// <summary> | |
/// Finds a type within the references of an assembly. | |
/// </summary> | |
/// <param name="module">The module to search the references of.</param> | |
/// <param name="type">The type to look for.</param> | |
/// <returns>The found type definition, or null if the type is not found.</returns> | |
/// <example>To find <see cref="System.Object"/> within a module. | |
/// <code>module.Find(typeof(System.Object));</code> | |
/// </example> | |
public static TypeDefinition Find(this ModuleDefinition module, Type type) | |
{ | |
if (module == null) | |
throw new ArgumentNullException("module", "module is null."); | |
if (type == null) | |
throw new ArgumentNullException("type", "type is null."); | |
var resolver = module.AssemblyResolver; | |
foreach (var assemblyRef in module.AssemblyReferences) | |
{ | |
var types = resolver.Resolve(assemblyRef).MainModule.Types; | |
foreach (var stype in types) | |
{ | |
if (stype.FullName == type.FullName) | |
return stype; | |
} | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment