Skip to content

[Mono] CustomAttributeTypedArgument.Value should return ReadOnlyCollection<CustomAttributeTypedArgument> for arrays #119292

@jkotas

Description

@jkotas

CustomAttributeTypedArgument.Value is documented to return ReadOnlyCollection<CustomAttributeTypedArgument> for arrays: https://learn.microsoft.com/en-us/dotnet/api/system.reflection.customattributetypedargument.value?view=net-9.0#remarks

For example, this is a standalone program which throws an InvalidCastException on Mono due this bug:

using System;
using System.Collections;
using System.Reflection;

using System.Runtime.InteropServices;

var attr = typeof(C).CustomAttributes.Single(d => d.AttributeType == typeof(A));
var arg = attr.ConstructorArguments.Single();
Console.WriteLine(arg.ToString());
class A : Attribute
{
    public unsafe A(params B<delegate*<void>[]>.E[] a) { }
} 

class B<T>
{
    public enum E { }
}

[A(new B<delegate*<void>[]>.E())]
unsafe class C { }

Output on Mono due to this bug:

Unhandled Exception:
System.InvalidCastException: Specified cast is not valid.
   at System.Reflection.CustomAttributeTypedArgument.ToString(Boolean typed)
   at System.Reflection.CustomAttributeTypedArgument.ToString()
   at Program.<Main>$(String[] args) in /home/venkad/extracts/Program.cs:line 9
[ERROR] FATAL UNHANDLED EXCEPTION: System.InvalidCastException: Specified cast is not valid.
   at System.Reflection.CustomAttributeTypedArgument.ToString(Boolean typed)
   at System.Reflection.CustomAttributeTypedArgument.ToString()
   at Program.<Main>$(String[] args) in /home/venkad/extracts/Program.cs:line 9

Expected output on CoreClr:

new B`1+E[[System.Void()[], System.Private.CoreLib, Version=10.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]][1] { 0 }

Output on Mono with the fix:

new B`1+E[[System.Void()[], System.Private.CoreLib, Version=10.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]][1] { 0 }

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions