Fix invalid handle bug happening when TypeBuilder
type used in exception catch clause
#106665
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PersistedAssemblyBuilder's
ILGenerator
cannot handle catch block with newly createdTypeBuilder
exception type, because theTypeBuilder
token is not valid whenILGenerator.BeginCatchBlock(Type? exceptionType)
called and the handle will not be populated properly until the assembly saved into a file or stream.The current solution for handling unpopulated tokens:
BlobBuilder.ReserveBytes(int)
will not work for this case asControlFlowBuilder.AddCatchRegion(...)
validates and reserves the exception type handle, not writes the token into BLOB directlySo instead of adding exception handler blocks directly into the
ControlFlowBuilder
instance collecting them into a list and adding them to theControlFlowBuilder
during Save where all tokens will be populated.Alternative solution: tried to fill valid
TypeBuilder
handles using reflection, but it's getting very messy, need to access and manipulate private/internal members ofControlFlowBuilder
.Fixes #106485