FreeOZ论坛

标题: How to write c# using Mono.cecil? [打印本页]

作者: DDD888    时间: 23-8-2013 21:51
标题: How to write c# using Mono.cecil?
本帖最后由 DDD888 于 23-8-2013 20:55 编辑

Good evening,

I need to write c# using Mono.cecil for new DateTime(2012, 1, 18)

Here is the il code.

IL_0000: ldc.i4 2012
IL_0005: ldc.i4.1
IL_0006: ldc.i4.s 18
IL_0008: newobj instance void [mscorlib]System.DateTime::.ctor(int32, int32, int32)
IL_000d: ret

Here is my partial c# code.

                    var body = new MethodBody(method);
                    var ins = body.Instructions;
                    ins.Add(Instruction.Create(OpCodes.Ldc_I4, 2012));
                    ins.Add(Instruction.Create(OpCodes.Ldc_I4_1));
                    ins.Add(Instruction.Create(OpCodes.Ldc_I4_S, 18));

I don't know how to write new obj instance statement.

                    ins.Add(Instruction.Create(OpCodes.Newobj,));

Do you know how to do that?

TIA

作者: cais    时间: 23-8-2013 23:31
what's the error msg that you saw? maybe you have to import the class first?
http://questiontrack.com/emit-ca ... .cecil-1123342.html
作者: DDD888    时间: 24-8-2013 16:25
cais 发表于 23-8-2013 22:31
what's the error msg that you saw? maybe you have to import the class first?
http://questiontrack.c ...

Thanks

I got it sorted out.

Here is the missing code.
                    var dateTimeConstructorMethod = typeof(DateTime).GetConstructor(new[] { typeof(int), typeof(int), typeof(int) });
                    var dateTimeConstructor = method.Module.Import(dateTimeConstructorMethod);
                    ins.Add(Instruction.Create(OpCodes.Newobj, dateTimeConstructor));





欢迎光临 FreeOZ论坛 (https://www.freeoz.org/bbs/) Powered by Discuz! X3.2