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

ResxSourceGenerator: Incorrect handling of non-string resources #7580

Open
mkaring opened this issue Feb 17, 2025 · 0 comments
Open

ResxSourceGenerator: Incorrect handling of non-string resources #7580

mkaring opened this issue Feb 17, 2025 · 0 comments

Comments

@mkaring
Copy link

mkaring commented Feb 17, 2025

Analyzer

Microsoft.CodeAnalysis.ResxSourceGenerator

Analyzer source

NuGet Package: Microsoft.CodeAnalysis.ResxSourceGenerator

Version: 3.11.0-beta1.24629.2

Describe the bug

The ResxSourceGenerator handles all non-string resources in the resx files incorrectly. Visual Studio allows to add images, text files and any other type of file into the resources.

This is represented in the resx file like this:

<root>
  <assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  <data name="favicon-dark" type="System.Resources.ResXFileRef, System.Windows.Forms">
    <value>favicon-dark.png;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
  </data>
</root>

The file is correctly embedded by the compiler into the resources file. How ever the property generated by the source generator incorrectly exposes the resource in this example as System.String instead of System.Byte[]. The compilation of the project works without issue, the error is raised at runtime when accessing the generated resource.

Steps To Reproduce

Create any project with a non-string resource in the resx file that is handled by the source generator. The resource needs to be of any type but string. You find that the property is created with the return type string, no matter what. Running the application that is accessing the resource, will give you a runtime error.

Alternatively you can use the attached project, that has already been prepared correctly: SerializationTest.zip

You find that the resource has been generated as string instead of byte[].

Expected behavior

Ideally the generator handles the non-string resources fully correctly and creates properties that match the expected type or a related one. (byte[] could be replaced with Memory or something liket this).

If that is not an option the generated should at least fail at compile time, to let the developer know that this is not going to work.

Actual behavior

The resource generator creates a properly with the wrong return type that causes a runtime error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants