리플렉션 내보내기를 사용하여 MSIL 명령 내보내기

업데이트: 2007년 11월

ILGenerator 클래스에서는 MSIL(Microsoft Intermediate Language)을 내보내는 데 사용할 수 있는 메서드를 제공합니다. ConstructorBuilder.GetILGenerator 메서드는 생성자에 대해 ILGenerator를 반환합니다. MethodBuilder.GetILGenerator 메서드는 메서드에 대해 ILGenerator를 반환합니다.

참고:

동적 메서드가 실행되면 JIT(Just-In-Time) 컴파일러가 호출되어 MSIL이 네이티브 코드로 변환됩니다. 이때 예외가 throw될 수 있습니다. 예를 들어, 메서드 본문이 최대 허용 크기와 같은 내부 한계를 초과하면 InvalidProgramException이 throw될 수 있습니다. 내보낸 MSIL을 강제로 확인하기 위해 SkipVerification이 거부되면 내부 VerificationException 예외와 함께 TargetInvocationException이 throw될 수 있습니다. 코드를 확인하면 프로그램의 안정성과 품질을 향상시킬 수 있으므로 개발 과정에서는 SkipVerification을 거부하는 것이 좋습니다.

ILGenerator 클래스에서는 다음 서비스를 제공합니다.

  • ILGenerator.Emit 메서드의 다양한 형식의 폼을 사용하여 다른 종류의 명령을 내보냅니다. 명령의 형식에 따라 이 명령에서 다른 종류의 피연산자를 사용할 수 있습니다.

  • 레이블을 선언합니다. 다양한 메서드를 사용하여 명령 스트림의 레이블 위치를 지정합니다.

  • MSIL 스트림의 위치를 레이블로 표시합니다.

  • 예외를 throw합니다.

  • 콘솔에 한 줄을 기록합니다.

  • 예외 블록을 정의합니다.

    • ILGenerator.BeginExceptionBlock에서 예외 블록을 시작합니다.

    • ILGenerator.BeginExceptFilterBlock에서 필터링된 예외 처리기를 시작합니다.

    • ILGenerator.BeginCatchBlock에서 입력된 예외 처리기를 시작합니다.

    • ILGenerator.BeginFinallyBlock에서 finally 처리기를 시작합니다.

    • ILGenerator.BeginFaultBlock에서 fault 처리기를 시작합니다.

    • ILGenerator.EndExceptionBlock에서 예외 블록을 종료합니다.

Catch 처리기의 경우 호출 순서는 다음 템플릿과 유사합니다.

Emit some MSIL.
BeginExceptionBlock
Emit the MSIL for the try block.
BeginCatchBlock
Emit the MSIL for the handler.
BeginCatchBlock
Emit the MSIL for the handler.
BeginFaultBlock
Emit the MSIL for the fault block.
BeginFinallyBlock
Emit the MSIL for the finally block.
EndExceptionBlock

필터링된 처리기의 경우 호출 순서는 다음 템플릿과 유사합니다.

Emit some MSIL.
BeginExceptionBlock
Emit the MSIL for the try block.
BeginExceptFilterBlock
Emit the MSIL for the filtered exception handler.
BeginCatchBlock
Emit the MSIL for the catch block. The catch handler should be supplied with a null type.
BeginFaultBlock
Emit the MSIL for the fault block.
BeginFinallyBlock
Emit the MSIL for the finally block.
EndExceptionBlock

참고 항목

기타 리소스

리플렉션 내보내기 사용