[c#] 메서드의 리플렉션 사용
리플렉션 사용 예시:
using System;
using System.Reflection;
public class Program
{
public static void Main()
{
Type type = typeof(MyClass);
MethodInfo methodInfo = type.GetMethod("MyMethod");
methodInfo.Invoke(new MyClass(), null);
}
}
public class MyClass
{
public void MyMethod()
{
Console.WriteLine("Hello, Reflection!");
}
}
위 예시에서, typeof
키워드를 사용하여 MyClass
의 Type
을 가져온 후, GetMethod
메서드로 MyMethod
의 MethodInfo
를 가져옵니다. 이후 Invoke
메서드를 사용하여 MyMethod
을 실행합니다.
리플렉션은 일부 상황에서 매우 유용하지만, 남용하면 코드를 이해하기 어려워지고 유지 보수가 어려워집니다. 따라서 리플렉션을 사용할 때에는 신중하게 고려해야 합니다.
리플렉션에 대해 더 배우고 싶다면, Microsoft 공식 문서를 확인해보시기를 권장합니다. 해당 문서에는 상세한 내용과 예시 코드가 포함되어 있습니다: 리플렉션 (C#)