Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ComponentModel Category attribute with UseSystemResourceKeys is broken #49554

Closed
eerhardt opened this issue Mar 12, 2021 · 3 comments · Fixed by #57227
Closed

ComponentModel Category attribute with UseSystemResourceKeys is broken #49554

eerhardt opened this issue Mar 12, 2021 · 3 comments · Fixed by #57227
Assignees
Labels
area-System.ComponentModel linkable-framework Issues associated with delivering a linker friendly framework
Milestone

Comments

@eerhardt
Copy link
Member

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.

@ghost
Copy link

ghost commented Mar 12, 2021

Tagging subscribers to this area: @safern
See info in area-owners.md if you want to be subscribed.

Issue Details

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.

Author: eerhardt
Assignees: joperezr
Labels:

area-System.ComponentModel

Milestone: -

@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged New issue has not been triaged by the area owner label Mar 12, 2021
@eerhardt eerhardt added the linkable-framework Issues associated with delivering a linker friendly framework label Mar 12, 2021
@ghost
Copy link

ghost commented Mar 12, 2021

Tagging subscribers to 'linkable-framework': @eerhardt, @vitek-karas, @LakshanF, @tannergooding, @sbomer
See info in area-owners.md if you want to be subscribed.

Issue Details

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.

Author: eerhardt
Assignees: joperezr
Labels:

area-System.ComponentModel, linkable-framework, untriaged

Milestone: -

@rogerge88
Copy link

We will follow your instruction and test it. Thanks

@maryamariyan maryamariyan removed the untriaged New issue has not been triaged by the area owner label Apr 8, 2021
@maryamariyan maryamariyan added this to the 6.0.0 milestone Apr 8, 2021
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Aug 11, 2021
@ghost ghost closed this as completed in #57227 Aug 12, 2021
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Aug 12, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Sep 11, 2021
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.ComponentModel linkable-framework Issues associated with delivering a linker friendly framework
Projects
None yet
4 participants