-
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
Accessing constructors with UnsafeAccessor throws exception on generic types in .NET9 #104879
Comments
Tagging subscribers to this area: @dotnet/area-system-runtime-compilerservices |
So most generic support was deemed broken in .NET 8. I tried to capture that in #89439. It clearly wasn't properly expressed nor was the fact that generic parameters now must match in location and number. The documentation for For context, this was part of type parameters must be type parameters and method parameters must be method parameters so signature matching was simpler. The result is that we match signatures precisely. For the above scenario it is relatively simple and we could make it work, but it quickly becomes much more complicated when generic parameters are involved. I'm inclined to report a breaking change issue here for .NET 8 instead. The workaround is to follow the supported generic way to handle this: static List<int> CallConstructorByUnsafeAccessor()
{
var c = (List<int>)RuntimeHelpers.GetUninitializedObject(typeof(List<int>));
Accessor<int>.CtorAsMethod(c);
return c;
}
class Accessor<T>
{
[UnsafeAccessor(UnsafeAccessorKind.Method, Name = ".ctor")]
public extern static void CtorAsMethod(List<T> c);
}
static void Main(string[] args)
{
var c = CallConstructorByUnsafeAccessor();
Console.WriteLine(c.Count);
} |
Re-reading through the history of the original feature PR, specifically here, this sounds like this could be considered a bug. I genuinely don't think that is fair so think a breaking change issue is more appropriate because it wasn't blocked in .NET 8. |
After some additional discussion, we've decide to document this as a breaking change. It can be tracked at dotnet/docs#41769. |
Description
Accessing constructors with UnsafeAccessor throws exception on generic types in .NET9 preview 6.
Reproduction Steps
Expected behavior
The code works on .NET 8 and .NET 9
Actual behavior
Also fails with NativeAOT
dotnet publish -r win-x64 -c Debug /p:PublishAot=true
Regression?
Yes as it works on .NET 8
Known Workarounds
No response
Configuration
No response
Other information
No response
The text was updated successfully, but these errors were encountered: