Skip to content
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

MissingMethodException when using new C# language features (in parameters, init-only setters, etc.) with generic methods, or with members in generic types #1148

Closed
stakx opened this issue Mar 15, 2021 · 5 comments

Comments

@stakx
Copy link
Contributor

stakx commented Mar 15, 2021

TL;DR: Upgrade to .NET 6+ (including Preview versions) to get your code working.

We've got quite a few reports lately of MissingMethodException happening for generic methods, or methods in generic types, when those methods make use of newer C# language features, e.g. in parameters, or init-only setters. I'm going to close all of those reports and pin this issue at the top of the issue tracker to make this info easier to find.

Some more background:

The issue is caused by a bug inside System.Reflection.Emit (dotnet/runtime#25958), which causes method signatures to be reproduced incorrectly if the original method signature happens to include a modreq, and if the method is either generic or in a generic type. Lately, the IL metadata encodings for new C# language features increasingly rely on such modreqs (see e.g. here for init-only setters, or here for in parameters), that's why they're affected by this bug.

The bad news is, because the bug is in the runtime, there's nothing we can do in Moq 4 to solve this problem. A limited workaround inside Castle DynamicProxy (the library underneath Moq) would be theoretically possible (castleproject/Core#430 (comment)) but very costly and tricky to implement, so that might not happen.

The good news is, the bug has since been fixed in .NET 6 (dotnet/runtime#40587) so once you upgrade, your code should once again work correctly.

@KinsonDigital

This comment has been minimized.

@stakx

This comment has been minimized.

@KinsonDigital

This comment has been minimized.

@KinsonDigital

This comment has been minimized.

@stakx
Copy link
Contributor Author

stakx commented Dec 30, 2022

.NET 6 has been out for fairly long and is now an LTS release, so this reminder to update to .NET 6 is perhaps no longer necessary. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant