Hey Daniel,
Are you sure this is going through this code path?
Looking at the code, it's doing some superfluous work, but it doesn't
look wrong per se. From the look of the exception, there's a
TypeReference for System.Exception declared in the original module
that ends up in the tree of the targetAssembly.
Also, any reason you couldn't write:
newInst = proc.Create(inst.OpCode, targetAssembly.MainModule.Import(def));
?
Jb
On Thu, Mar 13, 2014 at 4:10 PM, Daniel Wirtz <[email protected]> wrote:
> Hello everyone,
>
> I just started using Cecil and it's a really powerful tool. Though I have
> got an issue with importing exceptions from a try-catch-block properly. All
> the other types seem to work fine, but exceptions use to cause trouble.
>
> What I basically have is a chunk of IL from another assembly that throws an
> Excetion:
>
> IL_0000: nop
> IL_0001: nop
> IL_0002: ldstr "test"
> IL_0007: newobj System.Void System.Exception::.ctor(System.String)
> IL_000c: throw
> IL_000d: stloc.0
> ...
>
> I then export the instruction's operand, which is the
> Exception(String,String) constructor, and declaring type, which is
> System.Exception, through looking it up:
>
> MethodDefinition def = (inst.Operand as MethodReference).Resolve();
> if (def.IsConstructor) {
> TypeDefinition classDef = (inst.Operand as
> MethodReference).DeclaringType.Resolve();
> targetAssembly.MainModule.Import(classDef);
> MethodDefinition ctorDef = GetConstructor(classDef, def);
> newInst = proc.Create(inst.OpCode,
> targetAssembly.MainModule.Import(ctorDef));
> }
>
> This works so far but when I call targetAssembly.Write(...) it raises the
> following exception:
>
> Error: System.ArgumentException: Member 'System.Exception' is declared in
> another module and needs to be imported
> at Mono.Cecil.MetadataBuilder.LookupToken(IMetadataTokenProvider provider)
> in Mono.Cecil\AssemblyWriter.cs:line 1967.
>
> This doesn't seem to happen with anything else but exceptions. I'd be really
> happy for any ideas on what I might do wrong :-)
>
> --
> --
> --
> mono-cecil
> ---
> You received this message because you are subscribed to the Google Groups
> "mono-cecil" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
--
--
--
mono-cecil
---
You received this message because you are subscribed to the Google Groups
"mono-cecil" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.