-
Notifications
You must be signed in to change notification settings - Fork 4.5k
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
UnsafeAccessor throws MissingFieldException on generic types #104268
Comments
Tagging subscribers to this area: @dotnet/area-system-runtime-compilerservices |
Looks like a duplicate of #92633. |
Ah, sorry, I missed |
I believe this failure is expected. The design in #99468 requires that the "generic context" of the type and method that has the
This looks for a field on
This looks for a field of type Your workaround that works is the only one to meet the design of the feature. |
I understand that this feature was never meant to be intuitive but consider either changing the design to make it "just work" for this case or throw a more informative exception. |
I would add that if it is expected to fail, it would be best if it did so consistently.
|
What would be a more informative exception in this case? |
Avoid using "generic context" or "bound"/"unbound" |
That would be more helpful. It would also require substantially more analysis to determine "why" the generic parameter didn't match. Appreciate the complexity here, but as noted this is a very advanced feature. We did try to document proper usage in the official documentation. Can you review the documentation and perhaps suggest a way to add some clarity? |
It seems that using a specific type as the generic argument is always wrong and I assume that it's easy to detect.
It's mostly clear, though it doesn't mention how generic methods on generic types should be handled. |
This isn't quite correct. It is only correct if the generic argument is also the type of a field. For example, looking up the
The following was intended to capture that:
This is also captured in the use examples for the API. See Examples Snippet from the Examples section: public class Class<T>
{
private void GM<U>(U u) { }
}
class Accessors<V>
{
...
[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "GM")]
public extern static void CallGM<X>(Class<V> c, X x);
...
}
Not sure embedding links to official documentation for the type someone is using is appropriate. I would imagine if an API isn't doing what one expects, looking at the official documentation would be the step (1). Perhaps adding a comment about comparing signatures is how look up is done? |
Also consider showing the full signature being looked up in the exception message. |
The best we could get to here would be the metadata encoded signature, which means a sequence of bytes, there is no obvious way to reconstitute the C# the user wrote. I assume you are not suggesting we print out the full signature of each attempted match, so then we need some metric to determine which of the possible matches to print out. For methods this could get complicated. For fields, I suppose we could print only the one where the name matches, but we're still back at a byte array. I'm happy to update the official documentation about matching ECMA metadata signatures, but I'm not really convinced adding anything else to the exception message is going to be worth the effort. |
Description
UnsafeAccessor throws MissingFieldException on generic types
Reproduction Steps
Expected behavior
Gets field
Actual behavior
Unhandled exception. System.MissingFieldException: Field not found: 'DependentBase`1.k__BackingField'.
Regression?
No
Known Workarounds
This works:
But this doesn't:
Configuration
.NET SDK 9.0.100-preview.7.24351.2
Other information
No response
The text was updated successfully, but these errors were encountered: