Allow ReadOnlySpan
Attribute constructor arguments
#8475
-
Especially with This may be achievable by allowing attributes to have Examplepublic class Model
{
[My(null, "abc", 123)] public object MyValues { get; set; }
}
public class MyAttribute : Attribute
{
private List<object?> _parsedValues;
public MyAttribute(params object?[] rawValues) // allowed today
{
ArgumentNullException.ThrowIfNull(rawValues);
_parsedValues = ValidateAndParse(rawValues.AsSpan());
}
public MyAttribute(params ReadOnlySpan<object?> rawValues)
{
_parsedValues = ValidateAndParse(rawValues);
}
private static List<object?> ValidateAndParse(ReadOnlySpan<object?> rawValues)
{
// ...
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 8 replies
-
The compiled format of attribute belongs to dotnet/runtime. It can only be utilized by C# if the format is decided, and the runtime supports loading such attribute. |
Beta Was this translation helpful? Give feedback.
-
I'd honestly think that the cost associated with allocating an array for an attribute would be miniscule compared to the reflection to interpret the custom attributes. |
Beta Was this translation helpful? Give feedback.
I'd honestly think that the cost associated with allocating an array for an attribute would be miniscule compared to the reflection to interpret the custom attributes.