Hello Jb,

what I am actually doing is to read IL from one assembly and put it into 
another. For that I wrote a method to move the IL around, injecting 
required additional local vars, preserving stack and local variable orders, 
which all works just fine with this sole exception.

For normal methods I usually do exactly what you have suggested:

newInst = proc.Create(inst.OpCode, targetAssembly.MainModule.Import(def));

The code I posted includes lots of stuff I tried to circumvent the error.

But, more interesting, I also did a few more tests. What actually works is 
importing the following:

throw new Exception("test");

But this doesn't work:

try {
throw new Exception("test");
} catch (Exception ex) {
}

As this then throws the cecil exception from above. I am feeling somewhat 
lost right now :-)

Daniel

Am Donnerstag, 13. März 2014 17:29:29 UTC+1 schrieb Jb Evain:
>
> 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]<javascript:>> 
> 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] <javascript:>. 
> > 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.

Reply via email to