성태의 닷넷 이야기
홈 주인
모아 놓은 자료
프로그래밍
질문/답변
사용자 관리
사용자
메뉴
아티클
외부 아티클
유용한 코드
온라인 기능
MathJax 입력기
최근 덧글
[정성태] Java - How to use the Foreign Funct...
[정성태] 제가 큰 실수를 했군요. ^^; Delegate를 통한 Bein...
[정성태] Working with Rust Libraries from C#...
[정성태] Detecting blocking calls using asyn...
[정성태] 아쉽게도, 커뮤니티는 아니고 개인 블로그입니다. ^^
[정성태] 질문이 잘 이해가 안 됩니다. 우선, 해당 소스코드에서 ILis...
[양승조
] var대신 dinamic으로 선언해서 해결은 했습니다. 맞는 해...
[양승조
] 또 막혔습니다. ㅠㅠ var list = props[i].Ge...
[양승조
] 아. 감사합니다. 어제는 안됐던것 같은데....정신을 차려야겠네...
[정성태] "props[i].GetValue(props[i])" 코드에서 ...
글쓰기
제목
이름
암호
전자우편
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>
첨부파일
스팸 방지용 인증 번호
1024
(왼쪽의 숫자를 입력해야 합니다.)