静态构造函数创建[Mono.Cecil]

 小天 发布于 2023-02-08 16:26

我和我的项目一直在使用静态构造函数.我需要在类型""中添加一个静态构造函数,以便调用我的资源解密方法.

在gif下面你会看到我遇到的问题.

我还将包含代码段. 在此输入图像描述

创建cctor的代码:

MethodDefinition method = new MethodDefinition(
    ".cctor",
    Mono.Cecil.MethodAttributes.Private
    | Mono.Cecil.MethodAttributes.Static
    | Mono.Cecil.MethodAttributes.HideBySig
    | Mono.Cecil.MethodAttributes.SpecialName
    | Mono.Cecil.MethodAttributes.RTSpecialName,
    mod.Import(typeof(void))
); 

我也尝试将属性更改为与Yano完全相同.它不知何故永远不会奏效.通过"工作"我的意思是在DotNet Resolver中将其检测为静态构造函数.

这里有一些关于真实结果和预期结果的更多信息.

在此输入图像描述

我没有附加到我的入口点的ResolveEventHandler.我将它附加到应用程序,它正在被混淆,它位于""类型的静态构造函数中,该构造函数甚至在调用入口点之前执行.

应用程序资源已使用AES加密,并且不会被dotnet解析程序或其他反编译器识别为有效资源.我只是问为什么事件没有被触发,因为它应该在资源无效或丢失时被触发.正如您在示例中所看到的,应该在应用程序启动之前弹出消息框,但它永远不会(字符串加密对字符串进行加密,因此有点难以看到字符串存在).

任何帮助表示赞赏.

1 个回答
  • 用这个 :

    void AddConstructor(TypeDefinition type, MethodReference baseEmptyConstructor)
    {
        var methodAttributes = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName;
        var method = new MethodDefinition(".ctor", methodAttributes, ModuleDefinition.TypeSystem.Void);
        method.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg_0));
        method.Body.Instructions.Add(Instruction.Create(OpCodes.Call, baseEmptyConstructor));
        method.Body.Instructions.Add(Instruction.Create(OpCodes.Ret));
        type.Methods.Add(method);
    }
    

    你也可以参考:

    http://www.mono-project.com/Cecil:FAQ

    2023-02-08 16:28 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有