-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PersistedAssemblyBuilder
generates invalid IL when loading fields with generic type parameters
#110247
Comments
Tagging subscribers to this area: @dotnet/area-system-reflection |
Tagging subscribers to this area: @dotnet/area-system-reflection-emit |
Verified repro; also verified that .NET Framework's implementation of AssemblyBuilder.Save() works correctly (with |
I'm not sure this is a bug even though it works in .NET Framework with The example closes the declaring type and then obtains the field from that: il.Emit(OpCodes.Ldfld, typeof(C<int>).GetField("F")); I assume that instead the declaring type leaves its generic parameter open: il.Emit(OpCodes.Ldfld, typeof(C<>).GetField("F")); |
This looks like a bug to me.
This would not work. ldfld token has to point to instantiated generic type for the IL to be valid. |
Yeah after trying that, I see that using the unbound generic field it generates: |
Description
I have found a possible issue with the new
PersistedAssemblyBuilder
introduced in .NET 9 that causes invalid IL to be generated when loading fields of generic types that have a generic type parameter as the field type.When decompiling the generated IL using
ildasm
or similar tools, the field reference shows up as having the concrete type instead of the type parameter.Reproduction Steps
The following code reproduces the problem in .NET 9. It doesn't produce a working executable by itself since I did not set the entry point, so to execute code from the generated assembly, you would need to reference it by some other assembly.
Expected behavior
The following IL should be emitted:
(Above is IL generated when running the code example on .NET 4.8.1, which is why it is using
mscorlib
.)When the code is executed, the following output should be printed:
Actual behavior
The following IL is generated:
As you can see, the second line references the type
int32
directly, instead of the type parameter!0
. This causes the following exception to be thrown when this code is exectued:Regression?
API was newly introduced in .NET 9. On .NET 4.8.1, when using the regular
AssemblyBuilder
, it generates a working assembly.Known Workarounds
No response
Configuration
Other information
I asked a question on the topic on StackOverflow, a user provided an explanation which goes into more depth than I would have been able to.
The text was updated successfully, but these errors were encountered: