用户名  找回密码
 FreeOZ用户注册
查看: 1325|回复: 2
打印 上一主题 下一主题

[学习深造] How to write c# using Mono.cecil?

[复制链接]
跳转到指定楼层
1#
发表于 23-8-2013 20:51:49 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?FreeOZ用户注册

x
本帖最后由 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
回复  

举报

2#
发表于 23-8-2013 22:31:34 | 只看该作者
what's the error msg that you saw? maybe you have to import the class first?
http://questiontrack.com/emit-ca ... .cecil-1123342.html
回复  

举报

3#
 楼主| 发表于 24-8-2013 15:25:56 | 只看该作者

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

评分

参与人数 1威望 +20 收起 理由
cais + 20 恭喜你!

查看全部评分

回复  

举报

您需要登录后才可以回帖 登录 | FreeOZ用户注册

本版积分规则

小黑屋|手机版|Archiver|FreeOZ论坛

GMT+10, 24-5-2025 19:14 , Processed in 0.014590 second(s), 21 queries , Gzip On, Redis On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表