C# 如何在我自己被ConfuserEx弄糊涂的类中使用GetMethod?

C# 如何在我自己被ConfuserEx弄糊涂的类中使用GetMethod?,c#,reflection,getmethod,confuserex,C#,Reflection,Getmethod,Confuserex,我有自己的DLL,我用ConfuserEx保护它。 在ConfuserEx中,我使用“重命名”保护: <protection id="rename"> <argument name="mode" value="unicode" /> <argument name="renEnum" value="true" /> </protection> 这里问题开始了,因为即使是我自己的代码也无法找到并使用我的(受Co

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

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

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

我仍然不知道为什么不能直接创建所需的代理而不进行反射,但是如果您确实需要获取
方法信息,请尝试以下操作:

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
,而不是它的名称,因此应该不会出现任何问题。

我仍然不确定为什么不能直接创建所需的委托而不进行反射,但如果确实需要获得
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
,而不是它的名称,因此应该不会出现任何问题。

我通过在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
    }
}

我通过在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
    }
}

为什么需要使用反射来访问自己程序集的成员?因为我使用的是一个具有委托的外部类,我希望连接到该类以便调用我的方法。如果该方法是由外部类调用的,您可能应该这样做。您可以创建一个私有/内部方法的委托,并将其传递给另一个程序集,而无需进行反射。可访问性检查在创建委托实例时完成。为什么需要使用反射来访问自己程序集的成员?因为我使用的外部类具有委托,并且我希望连接到该类以便调用我的方法。如果该方法是由外部类调用的,您可能应该这样做。您可以创建一个私有/内部方法的委托,并将其传递给另一个程序集,而无需进行反射。可访问性检查是在创建代理实例时完成的。看起来我们提出了类似的想法:)谢谢您的帮助。看起来我们提出了类似的想法:)谢谢您的帮助。