Skip to content

ComponentModel Category attribute with UseSystemResourceKeys is broken #49554

Closed

Description

@eerhardt

Publish and run the following application with and without UseSystemResourceKeys being set:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>

</Project>
using System;
using System.ComponentModel;

namespace BugRepro
{
    class Program
    {
        static void Main()
        {
            Console.WriteLine(GetEnumCategory(AnEnum.Somthing));
        }
        
        public static string GetEnumCategory<T>(T enumValue)
            where T : struct, IConvertible
        {
            if (!typeof(T).IsEnum)
                return null;
            var enumCategory = enumValue.ToString();
            var fieldInfo = enumValue.GetType().GetField(enumValue.ToString());
            if (fieldInfo != null)
            {
                var attrs = fieldInfo.GetCustomAttributes(typeof(CategoryAttribute), false);
                if (attrs != null && attrs.Length > 0)
                {
                    enumCategory = ((CategoryAttribute)attrs[0]).Category;
                }
            }
            return enumCategory;
        }
    }

    public enum AnEnum
    {
        [Category("SomthingCat")]
        Somthing = 52,
    }
}
❯ dotnet publish -r win-x64 -p:PublishTrimmed=true -p:TrimMode=link -p:UseSystemResourceKeys=false
❯ C:\DotNetTest\BugRepro\bin\Debug\net5.0\win-x64\publish\BugRepro.exe
SomthingCat

❯ dotnet publish -r win-x64 -p:PublishTrimmed=true -p:TrimMode=link -p:UseSystemResourceKeys=true
❯ C:\DotNetTest\BugRepro\bin\Debug\net5.0\win-x64\publish\BugRepro.exe
PropertyCategorySomthingCat

The reason this happens is because of this code:

public string Category
{
get
{
if (!_localized)
{
lock (_locker)
{
string? localizedValue = GetLocalizedString(_categoryValue);
if (localizedValue != null)
{
_categoryValue = localizedValue;
}

If GetLocalizedString returns null, Category just returns what was passed to the ctor. But if GetLocalizedString doesn't return null, it uses what came from GetLocalizedString. Which looks like:

protected virtual string? GetLocalizedString(string value) => SR.GetResourceString("PropertyCategory" + value, null);

This is looking for resources in our assembly named PropertyCategoryXXX where XXX is the category name. For example:

<data name="PropertyCategoryText" xml:space="preserve">
<value>Text</value>
</data>
<data name="PropertyCategoryWindowStyle" xml:space="preserve">
<value>Window Style</value>
</data>

One option is to turn UseSystemResourceKeys off for this assembly. Or we could try to change the code somehow to do something different when UseSystemResourceKeys=true.

Activity

  1. ghost added
    untriaged
    on Mar 12, 2021
  2. added
    linkable-framework
    on Mar 12, 2021
  3. rogerge88 commented on Mar 12, 2021

    @rogerge88

    We will follow your instruction and test it. Thanks

  4. removed
    untriaged
    on Apr 8, 2021
  5. added this to the 6.0.0 milestone on Apr 8, 2021
  6. ghost added
    in-pr
    on Aug 11, 2021
  7. ghost closed this as completedin #57227on Aug 12, 2021
  8. ghost removed
    in-pr
    on Aug 12, 2021
  9. ghost locked as resolved and limited conversation to collaborators on Sep 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-System.ComponentModellinkable-framework

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions