[C#] System.Reflection 을 이용한 메소드명, 파라메터명, 타입 등 얻는 방법

2010. 1. 14. 14:17Coders

C#에서 System.Reflection 을 이용하여 메소드/파라메터명 등을 얻는 방법입니다.
업무를 하면서, 특정 메소드가 제가 직접 만드는 게 아닌 경우가 있어서 좀 찾아보고 적용해 봤습니다.
여길(http://dotnetpulse.blogspot.com) 참고했습니다.
System.Reflection, 참 흥미로운 기능이 많은 네임스페이스입니다.
상업용 프로그램은 소스 난수화에 신경 좀 써야할 듯.(아예 C++로 개발하거나 말이죠.)

- 하지만, 다 의미 없어요. 난수화니 암호화니 뭐니 해 봤자, 개발자가 들고 튀면 뭐 대책 없음이죠. ㅋ

  1. System.Type type = this.GetType();  //현재클래스의 타입을 얻는다.
  2. // 예를 들어, 클래스의 이름이 MySoju 라면, 다음과같이 작성해도 무방
  3. //Type type = typeof(MySoju);
  4. System.Reflection.MethodInfo[] methodInfos = type.GetMethods();
  5. string sMsg = string.Empty;
  6. foreach (System.Reflection.MethodInfo mi in methodInfos)
  7. {
  8.   // 해당 메소드의 리턴타입과 이름
  9.   sMsg += string.Format("\n{0} {1}(", mi.ReturnType.FullName, mi.Name);
  10.   foreach (System.Reflection.ParameterInfo pi in mi.GetParameters())
  11.   {
  12.     // 각 파라메타의 타입과 이름
  13.     sMsg += string.Format("{0} {1},", pi.ParameterType.FullName, pi.Name);
  14.   }
  15.   if (sMsg.EndsWith(","))
  16.     sMsg = sMsg.Substring(0, sMsg.Length - 1);
  17.   sMsg += ")";
  18. }
  19.  
  20. MessageBox.Show(sMsg);

그냥 메모해 놓는 겁니다. 뭐.