2017-05-03 1 views
0

런타임에이 클래스의 메서드 이름과 클래스 이름을 가져 오는 방법. 코드는 컴파일 된 다음 일부 오픈 소스 obfuscator를 사용하여 난독 화됩니다.런타임시 클래스 및 메서드 이름 가져 오기

내가 컴파일 및 난독 후 내 코드를 실행하면
MainClass -> A 
MainClass.Main -> A.a 
Test -> B 
Test.TestMethod -> B.a 

내가 얻을 : 그래서

B 
TestMethod 

nameof

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!"); 
    } 
} 

난독는 다음과 같이 클래스와 메소드의 이름을 바꿉니다 예를 들면 다음과 같습니다 클래스 이름에 대해 예상대로 작동하지만 메소드 이름에는 작동하지 않습니다. nameof은 어떻게 작동합니까? 런타임에 클래스 및 메서드의 난독 화 된 이름을 얻는 올바른 방법은 무엇입니까?

+0

왜 당신이 알 필요합니까? 일반적으로 게시하고자하는 API를 만들고 특정 유형을 난독 화하지 못하게하려는 경우 (예 : IEndpoint.Send (IMessage))를 제외하고는 문제가되지 않습니다. 어떤 경우에는 Obfuscator에게 특정 클래스 나 메소드를 제외 시키라고 알려줍니다. – MickyD

+0

nameof()가 컴파일 타임에 실행됩니다. 이를 위해 리플렉션을 사용할 수 있습니다. –

+0

'nameof'는 컴파일 타임 기능이므로 obfuscator가 소스 코드 재 작성을 수행하지 않는 한'nameof (Test) '가'B'를 산출해야한다는 것이 특히 이상합니다. 그럼에도 불구하고 런타임에 클래스의 이름을 원하면'typeof (Test) .Name'을 사용하십시오. –

답변

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!"); 
    } 
} 
관련 문제