runtime icon indicating copy to clipboard operation
runtime copied to clipboard

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

Open Venkad000 opened this issue 1 year ago • 3 comments

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

Venkad000 avatar Aug 11 '25 06:08 Venkad000

Tagging subscribers to this area: @dotnet/area-system-linq See info in area-owners.md if you want to be subscribed.

dotnet-policy-service avatar Aug 11 '25 06:08 dotnet-policy-service

/cc: @saitama951 @uweigand @giritrivedi

Venkad000 avatar Aug 11 '25 06:08 Venkad000

cc: @jkotas @steveisok @akoeplinger

saitama951 avatar Aug 11 '25 09:08 saitama951