Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.