Hi,

On Tue, Feb 21, 2012 at 10:48 PM, thargy <[email protected]> wrote:
> I understand how to retrieve type definitions from references using
> the Resolve() methods.

Right. You can read more about it here:

https://github.com/jbevain/cecil/wiki/Resolving

> However, to find other types the solution appears to be use Import(),
> which is certainly not what I want to do, as it would require me to
> have a link in my own code to the type.  It also suffers in that it
> might get the wrong type version for the assembly currently being
> instrumented.

Nope, Import is about creating references from other assemblies for
your module. Let say you need to inject code to Console.WriteLine, you
need to import a reference for this before using it inside your
module.

> My question is how do I resolve types from an AssemblyNameReference.
>
> Sorry, for the long winding prologue but I feel like it's a dumb
> question and want to show I made an effort, but I'm pretty stuck.

We use an IAssemblyResolver to resolve assembly names into an actual assembly.

You can write:

var resolver = new DefaultAssemblyResolver();
var systemName = AssemblyNameReference.Parse("System, Version=2.0.0.0,
PublicKeyToken=xxxx");

AssemblyDefinition system = resolve.Resolve(systemName);

and now you can browse the types of system.

It's exactly what's used when you use the Resolve methods of member references.

> In the reflection world the Assembly.Load can take a FQN, however
> AssemblyDefinition apparently only loads from a file or a stream.  I
> kind of get this (what if the linked assembly is not available the
> decompiler can't depend on it's availability), but I've seen code that
> can resolve from the GAC (again - can't get the AssemblyDefinition's
> though), and there is an AssemblyResolver (I've implemented a custom
> one as recommended by jb's comments on his plans to drop the global
> one).  Given that, there's surely some way to find an
> AssemblyDefinition from an AssemblyNameReference, and hence from there
> grab it's modules and finally a specific TypeReference (and even
> TypeDefinition via Resolve).

Yeah exactly, call Resolve on the assembly resolver, passing the
assembly name, and then you can simply browse the type definitions in
the assembly.

Jb

-- 
--
mono-cecil

Reply via email to