Skip to content

[Mono] Difference in the ConstructorArguments of a CustomAttribute between Mono and CoreCLR leads to a testcase failure in Roslyn. #118568

@Venkad000

Description

@Venkad000

Description

This testcase in Roslyn link fails in Mono with a System.InvalidCastException: Specified cast is not valid.
This is a standalone reproducible.

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(((IEnumerable)arg.Value).Cast<CustomAttributeTypedArgument>().SingleOrDefault().Value ?? "null");

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 { }

This is due to the difference in the way the constructorArguments are structured in Mono and CoreCLR which allows (IEnumerable)arg.Value).Cast<CustomAttributeTypedArgument>() to be done in CoreCLR but not on Mono.

Should the way it is structured be changed in Mono to match that of CoreCLR or should the testcase be disabled on Mono?

Expected Output

0

Actual Output

Unhandled Exception:
System.InvalidCastException: Specified cast is not valid.
   at System.Linq.Enumerable.CastICollectionIterator`1[[System.Reflection.CustomAttributeTypedArgument, System.Private.CoreLib, Version=10.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext()
   at System.Linq.Enumerable.TryGetSingle[CustomAttributeTypedArgument](IEnumerable`1 source, Boolean& found)
   at System.Linq.Enumerable.SingleOrDefault[CustomAttributeTypedArgument](IEnumerable`1 source)
   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.Linq.Enumerable.CastICollectionIterator`1[[System.Reflection.CustomAttributeTypedArgument, System.Private.CoreLib, Version=10.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext()
   at System.Linq.Enumerable.TryGetSingle[CustomAttributeTypedArgument](IEnumerable`1 source, Boolean& found)
   at System.Linq.Enumerable.SingleOrDefault[CustomAttributeTypedArgument](IEnumerable`1 source)
   at Program.<Main>$(String[] args) in /home/venkad/extracts/Program.cs:line 9

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions