I got it all fixed, thanks for all the help. :)

On 21 Aug, 10:09, UbbeLoL <[email protected]> wrote:
> Thank you very much for your answer Daniel, I've tried what you posted
> earlier, but when calling the "SimplifyMacros" method I get an
> ArgumentOutOfRangeException. I know this might not be very helpful
> information, so i'm posting the whole method i'm emitting here:
>
> ilProc.Emit(OpCodes.Call,
> asmDef.MainModule.Import(typeof(System.Reflection.Assembly).GetMethod("GetE 
> xecutingAssembly")));
>             ilProc.Emit(OpCodes.Ldstr, "str");
>             ilProc.Emit(OpCodes.Callvirt,
> asmDef.MainModule.Import(typeof(System.Reflection.Assembly).GetMethod("GetM 
> anifestResourceStream",
> new Type[]{ typeof(string) })));
>             ilProc.Emit(OpCodes.Stloc_0);
>             ilProc.Emit(OpCodes.Ldloc_0);
>             ilProc.Emit(OpCodes.Callvirt,
> asmDef.MainModule.Import(typeof(Stream).GetMethod("get_Length")));
>             ilProc.Emit(OpCodes.Conv_Ovf_I);
>             ilProc.Emit(OpCodes.Newarr,
> asmDef.MainModule.Import(typeof(Byte)));
>             ilProc.Emit(OpCodes.Stloc_1);
>             ilProc.Emit(OpCodes.Ldloc_0);
>             ilProc.Emit(OpCodes.Ldloc_1);
>             ilProc.Emit(OpCodes.Ldc_I4_0);
>             ilProc.Emit(OpCodes.Ldloc_1);
>             ilProc.Emit(OpCodes.Ldlen);
>             ilProc.Emit(OpCodes.Conv_I4);
>             ilProc.Emit(OpCodes.Callvirt,
> asmDef.MainModule.Import(typeof(Stream).GetMethod("Read", new Type[]
> { typeof(byte[]), typeof(Int32), typeof(Int32)})));
>             ilProc.Emit(OpCodes.Pop);
>             ilProc.Emit(OpCodes.Call,
> asmDef.MainModule.Import(typeof(Encoding).GetMethod("get_ASCII")));
>             ilProc.Emit(OpCodes.Ldloc_1);
>             ilProc.Emit(OpCodes.Callvirt,
> asmDef.MainModule.Import(typeof(Encoding).GetMethod("GetString", new
> Type[] { typeof(byte[]) })));
>             ilProc.Emit(OpCodes.Ldc_I4_1);
>             ilProc.Emit(OpCodes.Newarr,
> asmDef.MainModule.Import(typeof(Char)));
>             ilProc.Emit(OpCodes.Stloc_2);
>             ilProc.Emit(OpCodes.Ldloc_2);
>             ilProc.Emit(OpCodes.Ldc_I4_0);
>             ilProc.Emit(OpCodes.Ldc_I4, 124);
>             ilProc.Emit(OpCodes.Stelem_I2);
>             ilProc.Emit(OpCodes.Ldloc_2);
>             ilProc.Emit(OpCodes.Callvirt,
> asmDef.MainModule.Import(typeof(String).GetMethod("Split", new Type[]
> { typeof(char[]) })));
>             ilProc.Emit(OpCodes.Ldarg_1);
>             ilProc.Emit(OpCodes.Ldelem_Ref);
>             ilProc.Emit(OpCodes.Ret);
>
>             mDef.Body.SimplifyMacros();
>
> On 21 Aug, 09:59, Daniel Grunwald <[email protected]> wrote:
>
>
>
>
>
>
>
> > "ldc.i4.s" expects an argument of type sbyte.
>
> > But it might be better not to emit short forms yourself (especially with
> > branch instructions, it's tricky to know up ahead if the short form can
> > be used).
> > You can emit "ldc.i4" with argument of type int, and call
> > method.Body.SimplifyMacros() [from Mono.Cecil.Rocks] to convert all
> > instructions in the method to their short form, if possible.
>
> > Daniel
>
> > On 8/21/2011 18:42, UbbeLoL wrote:
>
> > > So i'm trying to create a small method for one of my applications
> > > through mono.cecil. I'm not good enough in IL to write it purely from
> > > my mind so I code the method first in C# use ILDasm to get the IL code
> > > and then work from there. And in ILDasm's output the opcode "Ldc.i4.s"
> > > is used with the operand 124, and I later read up on the internet that
> > > this opcode should be used for pushing integers ranging from -127 to
> > > 127.
>
> > > But no matter what I cast the operand it doesn't help, it always gives
> > > me an "opcode" ArgumentException when trying to emit the instruction.
>
> > > What should I do?
>
> > > Thanks,
> > > UbbeLoL
>
> >  signature.asc
> > < 1KVisaHämta

-- 
--
mono-cecil

Reply via email to