dotnet / dotnet/dotnet-api-docs

ILGenerator.BeginFinallyBlock example is invalid

Open
#3,592 6 comments 0 reactions 0 assignees View on GitHub
area-System.Reflection.Emit Pri3 untriaged
Dominant language
C#
Stars
949
Forks
1.7k
Avg merge
3d 27m
Merged PRs (30d)
49

Description

As discovered by @luboid in https://github.com/dotnet/coreclr/issues/14379#issuecomment-334959413, [the example code for `ILGenerator.BeginFinallyBlock`](https://docs.microsoft.com/en-us/dotnet/api/system.reflection.emit.ilgenerator.beginfinallyblock?view=netcore-2.0#Examples) contains branch out of try block and is thus invalid. When I run it on .Net Framework 4.7 in x64 mode or .Net Core 2.0 (after changing `myAppDomain.DefineDynamicAssembly` to `AssemblyBuilder.DefineDynamicAssembly` and removing the permissions attribute to make it compile on .Net Core 2.0), I get the following exception:

```
Unhandled Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidProgramException: Common Language Runtime detected an invalid program.
at Adder.DoAdd(Int32 , Int32 )
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
at System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args)
at ILGenerator_BeginFinallyBlock.Main() in C:\code\tmp\coreapp\Program.cs:line 109
```

I think the example should be changed, so that it produces valid IL.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.