Skip to content

Conversation

@Venkad000
Copy link
Contributor

CustomAttributeTypedArgument is structured differently in Mono and CoreClr and the function ToString() only takes into consideration how CoreClr's CustomAttributeTypedArgument is implemented and does a cast accordingly which causes it to fail with an InvalidCastException on Mono. So the casts should be runtime specific for it to not throw an InvalidCastException.

For Example, this is a standalone program which throws an InvalidCastException on Mono but not on CoreClr:

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 without the fix:

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

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 }

The function ToString() only takes into consideration how CoreClr's CustomAttributeTypedArgument
is implemented and does a cast accordingly which causes it to fail with an InvalidCastException
on Mono. So the casts should be runtime specific for it to not throw an InvalidCastException.
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Aug 21, 2025
@github-actions github-actions bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Aug 21, 2025
@Venkad000 Venkad000 changed the title [Mono] Fix CustomAttributeTypedArgument.ToString() throwing an exception on Mono Fix CustomAttributeTypedArgument.ToString() throwing an exception on Mono Aug 21, 2025
@jkotas jkotas added area-System.Reflection and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Aug 21, 2025
@dotnet-policy-service
Copy link
Contributor

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

if (ArgumentType.IsArray)
{
#if !MONO
IList<CustomAttributeTypedArgument> array = (IList<CustomAttributeTypedArgument>)Value!;
Copy link
Member

@jkotas jkotas Aug 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Should this issue be fixed in the implementation of Value property on Mono instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jkotas I couldn't figure out where to change the implementation of Value. Could you maybe help me point to it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect that the fix is needed in ves_icall_System_Reflection_RuntimeCustomAttributeData_ResolveArgumentsInternal or one of the methods that it calls.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have assigned this to copilot: #119293. Let's see whether it can figure it out.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can take a look at the commits in #119293 for some inspiration where to fix this. (The changes in that PR are not quite right - if nothing else the changes in the shared code need to be under MONO ifdef.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jkotas I can't seem to figure out a way to get this working. The change makes mono crash irrespective of whether I do it in the C# code or C code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jkotas Can we make the code under a MONO ifdef?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix should be fixing the implementation of Value property to follow the documented behavior.

I am ok with some under MONO ifdef if there is no better way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Reflection community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants