Thanks to visit codestin.com
Credit goes to github.com

Skip to content

CustomAttribute's typed argument value has incorrect type for enum arguments #10555

@radical

Description

@radical

Test case:

using System;
using System.Reflection;

namespace dm_play
{
    public enum Levels { one, two, three }

    [AttributeUsage(AttributeTargets.Method)]
    public class TestAttribute : Attribute
    {
        Levels this_level;
        public TestAttribute(Levels level) => this_level = level;
    }

    public class MainClass
    {
        public static void Main(string[] args)
        {
            var mi = typeof(MainClass).FindMembers(MemberTypes.Method, BindingFlags.Instance | BindingFlags.Public, (m, criteria) => m.Name == nameof(MethodWithAttribute), null);

            foreach (CustomAttributeData cad in ((MethodInfo)(mi[0])).CustomAttributes) {
                Console.WriteLine($"attr.Type: {cad.AttributeType}");

                foreach (CustomAttributeTypedArgument cata in cad.ConstructorArguments)
                    Console.WriteLine($"-> cata: {cata.ArgumentType}, val: {cata.Value}, type of val: {cata.Value.GetType()}");
            }
        }

        [TestAttribute(Levels.two)]
        public void MethodWithAttribute() => Console.WriteLine("Hello");
    }

}

Expected output: (works with 5.16.0)

$ mono dm_play.exe
attr.Type: dm_play.TestAttribute
-> cata: dm_play.Levels, val: two, type of val: dm_play.Levels

Actual output: (with master)

 $ ~/misc/bin/mono dm_play.exe
attr.Type: dm_play.TestAttribute
-> cata: dm_play.Levels, val: 1, type of val: System.Int32

The CustomAttributeTypedArgument.ArgumentType is the same, but the CustomAttributeTypedArgument.Value has different types:
5.16.x: dm_play.Levels
master: System.Int32

The commit that introduced this behavior: faaa9a4

This breaks msbuild tests being run with xunit.

cc: @marek-safar @MaximLipnin

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions