-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
JIT throws MissingMethodException compiling dynamic method that invokes init
property setter declared in generic class
#45102
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
Based on this doc it seems likely that our IL code gen has to be updated to include As to why it only fails in generic classes may be a bug/detail of the .NET 5 runtime. Is this actually a bug in reflection or |
@steveharter advocated for #40587 to be merged into 6.0 due to risk, it seems. |
@wzchua does [this](https://github.com/dotnet/runtime/issues/25958#issuecomment-543403476] describe a workaround that I could possibly implement to fix-up the generated IL so I can run on current versions of runtimes? |
Ya, .NET Framework has this bug as well. I tried using |
@AArnott no. That describes why the previous attempt at fixing the issue broke something. I thought SignatureHelper would work but it can't. There's a step where the class token is operated with the method signature to generate some token. It calls into the unmanaged part of the runtime. But I think setting the backing field directly should work but it's not the same as calling the setter. |
That would require some way to associate the backing field with the property setter, assuming there even is a backing field that setting would be equivalent. We should also consider adding API to |
This is probably super fragile but I extracted the code path a generic class + non-generic methodInfo takes for token generation |
Thanks for the workaround, @wzchua. Ya, reflecting into internals sounds quite fragile, as you say. @wzchua : Is there a (safer?) way to simply detect if your fix is present in a given runtime? Right now I'm changing MessagePack to not even try to support the scenario since it's broken, but I'd like it to "light up" properly when running on a fixed runtime (whether that's 6.0 or 5.0 after it's fixed). @steveharter : it turns out at least for VS's own purposes, we use messagepack settings that lead to it using |
@AArnott, there is really no other way other than generating a method, invoking it and catching the exception. |
Closing per feedback:
|
Description
Very strangely, when a property setter is defined as
init
in C# 9, and that property is declared in a generic type, then dynamic generated code throws when calling it.Repro
Configuration
.NET 5.0. Repros on .NET Framework 4.8 as well.
x64
Regression?
Not a regression, since the repro depends on
init
property setters which was only introduced in .NET 5.0.Other information
See MessagePack-CSharp/MessagePack-CSharp#1134 which tracks this within the MessagePack repo.
Why does .NET 5.0 throw at runtime as if the property setter was not defined? And why only when the class is generic?
If I change from
init
toset
(or from generic class to non-generic), the problem goes away, even though the emitted IL is identical. Theinit
setter andset
setter are nearly identical too. There's just one small bit of metadata on aninit
setter that's different (that messagepack doesn't care about):The problem does not repro when using
DynamicMethod
. It only fails when usingMethodBuilder
.The text was updated successfully, but these errors were encountered: