OK I know this may be really dumb, but I'm really not sure what the
best approach for type resolution outside the currently loaded module.

I understand how to retrieve type definitions from references using
the Resolve() methods.

I can also get some of the types from mscorlib using
ModuleDefinition.TypeSystem (took a while to find that), which get's
me to 'void' for example.

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.

I could use ModuleDefinition.GetTypeReferences(), however there's no
guarantee that the type I'm looking for has been referenced (even
though the assembly will have been).  For example System.Type in the
referenced core library.

What I need is to find the type based on the current module's assembly
references.

In the System.Type case, I could use
ModuleDefinition.TypeSystem.Corlib to get the AssemblyNameReference
for the referenced mscorlib (is this the recommended way?).  Or I
could look for it in ModuleDefinition.AssemblyReferences (seems a
waste).  Regardless, I'm left with an AssemblyNameReference.

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.

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).

I may be completely missing the point, but some guidance would be
gratefully received.

-- 
--
mono-cecil

Reply via email to