首页 > 解决方案 > 如何在我自己的被 ConfuserEx 混淆的类中使用 GetMethod?

问题描述

我有自己的 DLL,我使用 ConfuserEx 保护它。在 ConfuserEx 中,我使用了“重命名”保护:

<protection id="rename">
    <argument name="mode" value="unicode" />
    <argument name="renEnum" value="true" />        
</protection>    

这当然可以防止 DLL 查看代码,但我的类(我已将其作为 DLL 的一部分进行保护)使用:

MethodInfo mi = typeof(MyClass).GetMethod(nameof(MyStaticMethod), BindingFlags.Static | BindingFlags.NonPublic);

问题从这里开始,因为即使是我自己的代码也无法找到和使用我的(受 ConfuserEx 保护的)方法。我使用GetMethod来调用:Delegate.CreateDelegate。我能做些什么来解决这个问题?

标签: c#reflectiongetmethodconfuserex

解决方案


我仍然不确定为什么你不能直接创建你需要的委托而不进行反射,但如果你真的需要得到MethodInfo,请尝试执行以下操作:

using System;
using System.IO;

class Program
{
    static void Main(string[] args)
    {
        Thingy t = DoStuff;
        var mi = t.Method;
    }
    private delegate void Thingy(object sender, EventArgs e);
    private static void DoStuff(object sender, EventArgs e)
    {

    }
}

也就是说,使用您自己的与其他委托定义匹配的本地定义委托,直接在您的代码中创建它的实例,然后MethodInfo从该实例中提取。

此代码将使用方法标记DoStuff而不是其名称来识别,因此应该可以在混淆后毫无问题地存活。


推荐阅读