Skip to content

Instantly share code, notes, and snippets.

@distantcam
Created August 21, 2013 03:09
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 distantcam/6289949 to your computer and use it in GitHub Desktop.
Save distantcam/6289949 to your computer and use it in GitHub Desktop.
Mono.Cecil Extension Methods
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