我有我自己的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。我能做些什么来解决这个问题?
发布于 2018-12-11 10:49:07
我仍然不知道为什么不能直接创建不需要反射的委托,但是如果确实需要获得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-11 10:54:42
我通过在GetMethod和目标方法之间添加一个额外的“桥委托”来解决这个问题。然后,我使用的不是名称(MyStaticMethod),而是BridgeDelegate.Method.Name。我检查并正确地工作了。
示例解决方案:
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
}
}https://stackoverflow.com/questions/53721503
复制相似问题