성태의 닷넷 이야기
홈 주인
모아 놓은 자료
프로그래밍
질문/답변
사용자 관리
사용자
메뉴
아티클
외부 아티클
유용한 코드
온라인 기능
MathJax 입력기
최근 덧글
[정성태] Reordering on an Alpha processor ;...
[정성태] 공유 감사합니다. ^^ 참고로, WPF에서 WindowsF...
[Tom Lee] 답변 감사합니다. 나름의 해결책 연구해보고 여기에도 공유해봅니다...
[정성태] 아래의 글을 보면, MoveWindow 하면 될 듯한데요. ^^...
[Tom Lee] 안녕하세요 올려주신 글 참고하여 WPF 어플리케이션 안에 Uni...
[정성태] A graphical depiction of the steps ...
[정성태] 질문을 주셔서 출판사 측에 문의를 했습니다. 약 한 달 정도 후...
[Thorondor
] @정성태 개인 블로그인데도 거의 커뮤니티 급 인 것 같아요. 요...
[정성태] Roll A Lisp In C - Reading ; https...
[정성태] Java - How to use the Foreign Funct...
글쓰기
제목
이름
암호
전자우편
HTML
홈페이지
유형
제니퍼 .NET
닷넷
COM 개체 관련
스크립트
VC++
VS.NET IDE
Windows
Team Foundation Server
디버깅 기술
오류 유형
개발 환경 구성
웹
기타
Linux
Java
DDK
Math
Phone
Graphics
사물인터넷
부모글 보이기/감추기
내용
<div style='display: inline'> <h1 style='font-family: Malgun Gothic, Consolas; font-size: 20pt; color: #006699; text-align: center; font-weight: bold'>GetFunctionPointer 호출 시 System.InvalidProgramException 예외 발생</h1> <p> 현상은 간단합니다. 제네릭 메서드인 경우 그에 대해 GetFunctionPointer를 호출하면,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > using System; using System.Reflection; using System.Threading; public class Program { <span style='color: blue; font-weight: bold'>public static void GenericMethod<T>(T obj)</span> { Thread.Sleep(1); } static unsafe void Main() { Type type = typeof(Program); MethodInfo mi = type.GetMethod("GenericMethod", BindingFlags.Static | BindingFlags.Public); { string fullName = string.Format("{0}.{1}", type.FullName, mi.Name); IntPtr methodBody = <span style='color: blue; font-weight: bold'>mi.MethodHandle.GetFunctionPointer(); // 예외 발생</span> Console.WriteLine(fullName); } } } </pre> <br /> MethodInfo.MethodHandle.GetFunctionPointer 메서드 호출에서 다음과 같은 예외가 발생합니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > An unhandled exception of type 'System.InvalidProgramException' occurred in mscorlib.dll Additional information: Common Language Runtime detected an invalid program. </pre> <br /> 이게... 이상한 듯 하면서도 사실 당연한 겁니다. 왜냐하면, 제네릭인 경우 컴파일러가 생성한 IL 단계에는 제네릭의 타입이 정해지지 않은 상태이고, 실제 메서드가 사용될 때 기계어 컴파일이 타입에 따라 확장되면서 컴파일되기 때문에 GenericMethod 자체의 FunctionPointer 값을 대표할 수 없는 것입니다. 가령 그 값이 0x00100으로 반환되었다고 해도 GenericMethod<int>(int obj)로 확장된 메서드의 FunctionPointer는 또 다른 값이 될 수 있는 것입니다.<br /> <br /> 그래서, 원래는 제네릭 메서드의 제대로 된 FunctionPointer를 구하고 싶다면 다음과 같이 해줘야 합니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > MethodInfo mi = type.GetMethod("GenericMethod", BindingFlags.Static | BindingFlags.Public); MethodInfo intMethod = <span style='color: blue; font-weight: bold'>mi.<a target='tab' href='https://docs.microsoft.com/en-us/dotnet/api/system.reflection.methodinfo.makegenericmethod'>MakeGenericMethod</a>(typeof(int));</span> string fullName = string.Format("{0}.{1}", type.FullName, intMethod.Name); IntPtr methodBody = intMethod.MethodHandle.GetFunctionPointer(); Console.WriteLine(fullName); // Program.GenericMethod </pre> <br /> 그런데, 재미있는 것은 클래스 수준의 제네릭 인자가 있는 것은 또 잘됩니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > public class GenericClass<T> { public static void Test(T arg) { Console.WriteLine(arg); } } type = typeof(GenericClass<>); mi = type.GetMethod("Test", BindingFlags.Static | BindingFlags.Public); { string fullName = string.Format("{0}.{1}", type.FullName, mi.Name); IntPtr methodBody = mi.MethodHandle.GetFunctionPointer(); Console.WriteLine(fullName); // GenericClass`1.Test } </pre> </p><br /> <br /><hr /><span style='color: Maroon'>[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]</span> </div>
첨부파일
스팸 방지용 인증 번호
1876
(왼쪽의 숫자를 입력해야 합니다.)