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

Skip to content

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

@radical

Description

@radical

This is a variation of the earlier issue #10555 with the difference that the custom attribute constructor accepts an object[] now.

Test case:

using System;
using System.Reflection;
using System.Collections.Generic;

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

    [AttributeUsage(AttributeTargets.Method)]
    public class TestAttribute : Attribute
    {
        object[] o;
        public TestAttribute(params object[] o) => this.o = o;
    }

    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) {
                    var value = cata.Value;
                    if (value is IEnumerable<CustomAttributeTypedArgument>) {
                        foreach (var cc in ((IEnumerable<CustomAttributeTypedArgument>)value))
                            PrintArgs(cc);
                    } else {
                        PrintArgs(cata);
                    }
                }
            }

            void PrintArgs(CustomAttributeTypedArgument cata) =>
                    Console.WriteLine($"-> argType: {cata.ArgumentType}, val: {cata.Value}, type of val: {cata.Value.GetType()}, argTypeIsEnum: {cata.ArgumentType.GetTypeInfo().IsEnum}");
        }

        [TestAttribute(Levels.one)]
        public void MethodWithAttribute() => Console.WriteLine("Hello");
    }
}
.net:
   -> argType: dm_play.Levels, val: 0, type of val: System.Int32, argTypeIsEnum: True

master (5.21.0.22 master/ee90fb094ec):
   -> argType: System.Object, val: 0, type of val: System.Int32, argTypeIsEnum: False

This is causing msbuild tests to fail on the PR: #10339

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions