-
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
[API Proposal]: Create array from array type #76478
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. |
Tagging subscribers to this area: @dotnet/area-system-runtime Issue DetailsBackground and motivationThe existing reflection APIs for creating array instances take array element type. If one has the actual array type available, this is both inefficient (requires going from the array type to the element type and back to the array type that we have started with) and requires suppressing warnings about the array type not being present with Aot. The proposal is the introduce a set of APIs that mirror the existing APIs for create array instances, except that they take the array type instead of element type. API Proposalnamespace System;
public class Array
{
// Proposed APIs
public static System.Array CreateInstanceFromArrayType(System.Type arrayType, int length);
public static System.Array CreateInstanceFromArrayType(System.Type arrayType, int length1, int length2);
public static System.Array CreateInstanceFromArrayType(System.Type arrayType, int length1, int length2, int length3);
public static System.Array CreateInstanceFromArrayType(System.Type arrayType, params int[] lengths);
public static System.Array CreateInstanceFromArrayType(System.Type arrayType, int[] lengths, int[] lowerBounds);
public static System.Array CreateInstanceFromArrayType(System.Type arrayType, params long[] lengths);
// Existing APIs
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("The code for an array of the specified type might not be available.")]
public static System.Array CreateInstance(System.Type elementType, int length);
public static System.Array CreateInstance(System.Type elementType, int length1, int length2);
public static System.Array CreateInstance(System.Type elementType, int length1, int length2, int length3);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("The code for an array of the specified type might not be available.")]
public static System.Array CreateInstance(System.Type elementType, params int[] lengths);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("The code for an array of the specified type might not be available.")]
public static System.Array CreateInstance(System.Type elementType, int[] lengths, int[] lowerBounds);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("The code for an array of the specified type might not be available.")]
public static System.Array CreateInstance(System.Type elementType, params long[] lengths);
} API UsageWe can replace
Alternative DesignsIntroduce overload for single dimensional arrays only. The single dimensional arrays are likely the 99+% use case for this API. RisksNo response
|
cc @dotnet/ilc-contrib Does this look reasonable? |
This comment was marked as resolved.
This comment was marked as resolved.
Looks reasonable to me! I'll leave it up to the API review board to think about the naming or the location of the API (we could also consider stashing it somewhere else, such as on Activator (add another overload of CreateInstance?), or RuntimeHelpers (GetUninitializedArray?). |
Looks good as proposed. The implementer is encouraged to consider whether all of these overloads are actually required, as the suggestion that the first overload will be in excess of 99% of all calls seems correct. namespace System;
public partial class Array
{
public static System.Array CreateInstanceFromArrayType(System.Type arrayType, int length);
public static System.Array CreateInstanceFromArrayType(System.Type arrayType, int length1, int length2);
public static System.Array CreateInstanceFromArrayType(System.Type arrayType, int length1, int length2, int length3);
public static System.Array CreateInstanceFromArrayType(System.Type arrayType, params int[] lengths);
public static System.Array CreateInstanceFromArrayType(System.Type arrayType, int[] lengths, int[] lowerBounds);
public static System.Array CreateInstanceFromArrayType(System.Type arrayType, params long[] lengths);
} |
@bartonjs I decided to implement only the next overloads: namespace System;
public partial class Array
{
public static System.Array CreateInstanceFromArrayType(System.Type arrayType, int length);
public static System.Array CreateInstanceFromArrayType(System.Type arrayType, params int[] lengths);
public static System.Array CreateInstanceFromArrayType(System.Type arrayType, int[] lengths, int[] lowerBounds);
} And I have not implemented the following overloads: namespace System;
public partial class Array
{
// It is not used in the current code and overload with `params int[] lengths` can be used instead
public static System.Array CreateInstanceFromArrayType(System.Type arrayType, int length1, int length2);
// It is not used in the current code and overload with `params int[] lengths` can be used instead
public static System.Array CreateInstanceFromArrayType(System.Type arrayType, int length1, int length2, int length3);
// Array does not support lengths more than `int.Max`. Array always uses `int` for lengths, not `long`
public static System.Array CreateInstanceFromArrayType(System.Type arrayType, params long[] lengths);
} |
Not going to land in .NET 8. This can effectively be done at any time after main opens for .NET 9 next month |
Fixed by #88620 |
Background and motivation
The existing reflection APIs for creating array instances take array element type. If one has the actual array type available, this is both inefficient (requires going from the array type to the element type and back to the array type that we have started with) and requires suppressing warnings about the array type potentially not being available with Aot.
The proposal is the introduce a set of APIs that mirror the existing APIs for creating array instances, except that they take the array type instead of the element type.
API Proposal
API Usage
We can replace
Array.CreateInstance(arrayType.GetElementType()!, length)
pattern in number of places in dotnet/runtime libraries withArray.CreateInstanceFromArrayType(arrayType, length)
:runtime/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/SurrogateDataContract.cs
Line 71 in e461b77
runtime/src/libraries/System.Private.Xml/src/System/Xml/Serialization/ReflectionXmlSerializationReader.cs
Line 2031 in 983c8f2
Alternative Designs
Introduce overload for single dimensional arrays only. The single dimensional arrays are likely the 99+% use case for this API.
Risks
No response
The text was updated successfully, but these errors were encountered: