Skip to content

Attributes taking object as a parameter have unexpected behavior in AOT #100688

@phil-scott-78

Description

@phil-scott-78

Description

Given an attribute that take an object as a parameter, when using an Enum it gets converted to an int before being passed to the constructor during AOT

Reproduction Steps

using System.ComponentModel;
using System.Reflection;

var defaultValue = typeof(MyTeam)
    .GetProperty("Quarterback")!
    .GetCustomAttribute<DefaultValueAttribute>()!.Value!;


var customValue = typeof(MyTeam)
    .GetProperty("Backup")!
    .GetCustomAttribute<MyCustomAttribute>()!.Value!;

Console.WriteLine($"{defaultValue.GetType()} {defaultValue}");
Console.WriteLine($"{customValue.GetType()} {customValue}");

internal enum Quarterbacks { Teddy, Lamar, Malik, Stefan }

internal class MyTeam
{
    [DefaultValue(Quarterbacks.Lamar)]
    public Quarterbacks Quarterback { get; set; }
    
    [MyCustom(Quarterbacks.Teddy)]
    public Quarterbacks Backup { get; set; }
}

internal class MyCustomAttribute(object value) : Attribute
{
    public object Value => value;
}

Expected behavior

Quarterbacks Lamar
Quarterbacks Teddy

Actual behavior

System.Int32 1
System.Int32 0

Regression?

Not sure

Known Workarounds

For custom attributes, using a generic attribute will now work. But that doesn't help when wanting to use an existing one like DefaultAttribute

Configuration

  • .net 8
  • Windows 11
  • x64

Not sure

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions