如何在运行时获取类的名称和此类的方法名称。代码被编译,然后使用一些开源混淆器进行模糊处理。这是一个例子:
class MainClass {
public static void Main(string[] args) {
Console.WriteLine(nameof(Test));
Console.WriteLine(nameof(Test.TestMethod));
Console.ReadLine();
}
}
class Test {
public static void TestMethod() {
Console.WriteLine("Hello World!");
}
}
混淆器重命名这样的类和方法:
MainClass -> A
MainClass.Main -> A.a
Test -> B
Test.TestMethod -> B.a
当我在编译和混淆后运行代码时,我得到:
B
TestMethod
因此nameof
按类预期的方式工作,但不适用于方法名称。 nameof
如何运作?在运行时获取类和方法的混淆名称的正确方法是什么?
答案 0 :(得分:0)
使用以下内容:
class MainClass {
public static void Main(string[] args) {
var methodinfo = typeof(Test).GetMethod("TestMethod");
var handle = methodinfo.MetaDataToken;
MethodBase method = System.Reflection.MethodBase.GetMethodFromHandle(handle);
string methodName = method.Name;
string className = method.ReflectedType.Name;
string fullMethodName = className + "." + methodName;
Console.WriteLine(fullMethodName);
Console.ReadLine();
}
}
class Test {
public static void TestMethod() {
Console.WriteLine("Hello World!");
}
}