0

我有自己的 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。我能做些什么来解决这个问题?

4

2 回答 2

2

我仍然不确定为什么你不能直接创建你需要的委托而不进行反射,但如果你真的需要得到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而不是其名称来识别,因此应该可以在混淆后毫无问题地存活。

于 2018-12-11T10:49:07.550 回答
1

我通过在 GetMethod 和目标方法之间应用额外的“桥委托”解决了这个问题。然后,我使用 BridgeDelegate.Method.Name 而不是 nameof (MyStaticMethod)。我检查并正常工作。

示例解决方案:

internal static class MyClass
{
    private delegate void ExecuteObfuscatedMethod(string value);
    private static ExecuteObfuscatedMethod Bridge; //This is my "bridge"

    internal static void CaptureExternalDelegate(object source)
    {
        //Using a "bridge" instead of the direct method name
        MethodInfo mi = typeof(MyClass).GetMethod(Bridge.Method.Name, BindingFlags.Static | BindingFlags.NonPublic);

        //Less interesting code
        PropertyInfo p = source.GetType().GetProperty("SomePrivateDelegate", BindingFlags.NonPublic | BindingFlags.Instance);
        Delegate del = Delegate.CreateDelegate(p.PropertyType, mi) as Delegate;
        Delegate original = p.GetValue(source) as Delegate;
        Delegate combined = Delegate.Combine(original, del);
        p.SetValue(property, combined);
    }

    static MyClass()
    {
        Bridge += MyStaticMethod;
    }

    //This is the method whose name can not be retrieved by nameof () after applying ConfuserEx
    private static void MyStaticMethod(string value)
    {
        //I am testing the method's name after calling it.
        var st = new StackTrace();
        var sf = st.GetFrame(0);
        var currentMethodName = sf.GetMethod();
        throw new Exception("The method name is: " + currentMethodName); //You can see that the method has evoked and you can even see its new name
    }
}
于 2018-12-11T10:54:42.927 回答