Skip to content

Type.GetProperties(Public | Static) should only need DynamicallyAccessedMemberTypes.PublicProperties #1724

Closed
dotnet/runtime
#48536
@eerhardt

Description

@eerhardt

Run the following code through the ILLinker:

        private static Dictionary<string, Color> GetColors()
        {
            var colors = new Dictionary<string, Color>(StringComparer.OrdinalIgnoreCase);
            FillWithProperties(colors, typeof(Color));
            FillWithProperties(colors, typeof(SystemColors));
            return colors;
        }

        private static void FillWithProperties(
            Dictionary<string, Color> dictionary,
            [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] Type typeWithColors)
        {
            foreach (PropertyInfo prop in typeWithColors.GetProperties(BindingFlags.Public | BindingFlags.Static))
            {
                if (prop.PropertyType == typeof(Color))
                    dictionary[prop.Name] = (Color)prop.GetValue(null, null)!;
            }
        }

Expected

Shouldn't have any warnings since typeWithColors is attributed as DynamicallyAccessedMemberTypes.PublicProperties.

Actual

Trim analysis warning IL2070: ConsoleApp2.Program.FillWithProperties(Dictionary<String,Color>,Type): The requirements declared via the 'DynamicallyAccessedMembersAttribute' on the parameter 'typeWithColors' of method 'ConsoleApp2.Program.FillWithProperties(Dictionary<String,Color>,Type)' don't match those on the implicit 'this' parameter of method 'System.Type.GetProperties(BindingFlags)'. The source value must declare at least the same requirements as those declared on the target location it's assigned to

cc @vitek-karas @MichalStrehovsky

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions