Microsoft MVP성태의 닷넷 이야기
오류 유형: 909. C# - DynamicMethod 사용 시 System.TypeAccessException [링크 복사], [링크+제목 복사],
조회: 5310
글쓴 사람
정성태 (seongtaejeong at gmail.com)
홈페이지
첨부 파일
 

C# - DynamicMethod 사용 시 System.TypeAccessException

지난 예제처럼 DynamicMethod를 사용하는 중에,

DynamicMethod dm = new DynamicMethod(
    Guid.NewGuid().ToString(),
    typeof(bool), [typeof(object)], typeof(Program), false);

특정 상황에서 이런 오류가 발생할 수 있습니다.

System.TypeAccessException
  HResult=0x80131543
  Message=Attempt by method 'DynamicClass.761a73f4-9126-4dd0-b3af-a5ff065f0d75(System.Object)' to access type 'System.Threading.Tasks.Task`1<MySql.Data.MySqlClient.MySqlPool>' failed.

이렇게 TypeAccessException가 발생하는 것은 DynamicMethod의 마지막 인자인 skipVisibility를 true로 주면 해결할 수 있습니다.

DynamicMethod dm = new DynamicMethod(
    Guid.NewGuid().ToString(),
    typeof(object), [typeof(object)], typeof(Program), true);

재현 코드가 좀 복잡해서 생략합니다. ^^;




CreateDelegate를 호출하는 중에,

resultFunc = (Func<object, bool>)dm.CreateDelegate(typeof(Func<object, object>));

"System.ArgumentException: 'Cannot bind to the target method because its signature is not compatible with that of the delegate type.'" 오류가 발생한다면?

뚫어지게 ^^ Signature를 확인해 보시면 됩니다. 간혹 (저처럼) 실수를 할 수도 있는데,

DynamicMethod dm = new DynamicMethod(
    Guid.NewGuid().ToString(),
    typeof(bool), [typeof(object)], typeof(Program), false);

resultFunc = (Func<object, bool>)dm.CreateDelegate(typeof(Func<object, object>));

저렇게 다른 signature를 부여했을 때 예외가 발생합니다. 자신의 코드를 의심하지 않으면 도대체 왜 저런 오류가 발생했는지 ^^; 파악이 어려운 경우입니다.




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]







[최초 등록일: ]
[최종 수정일: 6/17/2024]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




1  2  3  4  5  6  [7]  8  9  10  11  12  13  14  15  ...
NoWriterDateCnt.TitleFile(s)
13721정성태8/30/20244176C/C++: 168. Visual C++ CRT(C Runtime DLL: msvcr...dll)에 대한 의존성 제거 - 두 번째 이야기
13720정성태8/29/20244038VS.NET IDE: 193. C# - Visual Studio의 자식 프로세스 디버깅
13719정성태8/28/20244319Linux: 79. C++ - pthread_mutexattr_destroy가 없다면 메모리 누수가 발생할까요?
13718정성태8/27/20244692오류 유형: 921. Visual C++ - error C1083: Cannot open include file: 'float.h': No such file or directory [2]
13717정성태8/26/20244427VS.NET IDE: 192. Visual Studio 2022 - Windows XP / 2003용 C/C++ 프로젝트 빌드
13716정성태8/21/20244320C/C++: 167. Visual C++ - 윈도우 환경에서 _execv 동작
13715정성태8/19/20244533Linux: 78. 리눅스 C/C++ - 특정 버전의 glibc 빌드 (docker-glibc-builder)
13714정성태8/19/20244832닷넷: 2295. C# 12 - 기본 생성자(Primary constructors) (책 오타 수정) [3]
13713정성태8/16/20245081개발 환경 구성: 721. WSL 2에서의 Hyper-V Socket 연동
13712정성태8/14/20245079개발 환경 구성: 720. Synology NAS - docker 원격 제어를 위한 TCP 바인딩 추가
13711정성태8/13/20245902Linux: 77. C# / Linux - zombie process (defunct process) [1]파일 다운로드1
13710정성태8/8/20245627닷넷: 2294. C# 13 - (6) iterator 또는 비동기 메서드에서 ref와 unsafe 사용을 부분적으로 허용파일 다운로드1
13709정성태8/7/20245336닷넷: 2293. C# - safe/unsafe 문맥에 대한 C# 13의 (하위 호환을 깨는) 변화파일 다운로드1
13708정성태8/7/20245062개발 환경 구성: 719. ffmpeg / YoutubeExplode - mp4 동영상 파일로부터 Audio 파일 추출
13707정성태8/6/20245455닷넷: 2292. C# - 자식 프로세스의 출력이 4,096보다 많은 경우 Process.WaitForExit 호출 시 hang 현상파일 다운로드1
13706정성태8/5/20245809개발 환경 구성: 718. Hyper-V - 리눅스 VM에 새로운 디스크 추가
13705정성태8/4/20246016닷넷: 2291. C# 13 - (5) params 인자 타입으로 컬렉션 허용 [2]파일 다운로드1
13704정성태8/2/20245553닷넷: 2290. C# - 간이 dotnet-dump 프로그램 만들기파일 다운로드1
13703정성태8/1/20245780닷넷: 2289. "dotnet-dump ps" 명령어가 닷넷 프로세스를 찾는 방법
13702정성태7/31/20245656닷넷: 2288. Collection 식을 지원하는 사용자 정의 타입을 CollectionBuilder 특성으로 성능 보완파일 다운로드1
13701정성태7/30/20245687닷넷: 2287. C# 13 - (4) Indexer를 이용한 개체 초기화 구문에서 System.Index 연산자 허용파일 다운로드1
13700정성태7/29/20245246디버깅 기술: 200. DLL Export/Import의 Hint 의미
13699정성태7/27/20245737닷넷: 2286. C# 13 - (3) Monitor를 대체할 Lock 타입파일 다운로드1
13698정성태7/27/20245495닷넷: 2285. C# - async 메서드에서의 System.Threading.Lock 잠금 처리파일 다운로드1
13697정성태7/26/20245786닷넷: 2284. C# - async 메서드에서의 lock/Monitor.Enter/Exit 잠금 처리파일 다운로드1
13696정성태7/26/20245447오류 유형: 920. dotnet publish - error NETSDK1047: Assets file '...\obj\project.assets.json' doesn't have a target for '...'
1  2  3  4  5  6  [7]  8  9  10  11  12  13  14  15  ...