meziantou/Meziantou.Framework

Does your resx source generator support generating for binary resources?

Closed this issue · 7 comments

I sadly need to include an source generator for Windows Desktop applications in my own Sdk. Currently I use Microsoft's for non-WindowsDesktop projects for string based resources only. However I want to do support for both for the case of Windows Forms or WPF projects so they do not have to commit the ones generated by the default ResXFileCodeGenerator for their program's resources.

I also want to make it to where they do not have to set it up themselves as well if possible.

Binary files in resx are supported and exposed as byte[]

So no System.Drawing.Image or System.Drawing.Icons supported yet.

Maybe it could be added. What would be the generated code for Image and Icon? Ans how the code would be used in a WinForms project?

For Icon the function would look like so:

internal static System.Drawing.Icon MyIcon {
    get {
        object obj = ResourceManager.GetObject("MyIcon", resourceCulture);
        return ((System.Drawing.Icon)(obj));
    }
}

For Image Bitmap (sorry it was bitmap) it does:

internal static System.Drawing.Bitmap MyImage {
    get {
        object obj = ResourceManager.GetObject("MyImage", resourceCulture);
        return ((System.Drawing.Bitmap)(obj));
    }
}

This is how the default resx generator in windows forms programs generate the code for the resources with the ResXFileCodeGenerator which only ever gets regenerated when the resx file is changed (in a specific way from within the Visual Studio IDE).

And this is how a vast majority of people using Windows Forms have in their Properties/Resources.Designer.cs files where they also set an application icon (setting it inserts it into the Properties/Resources.resx file and also gets code generated). However if the code file backend can be source generated with this generator instead just how that generates it, it would all be great.

I've done a quick test in a WinForms project. The resx editor correctly set the type of the binary resource to Bitmap. Here's the content of the resx file:

  <data name="Image1" type="System.Resources.ResXFileRef, System.Windows.Forms">
    <value>Resources\Image1.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
  </data>

In this case, the source generator produces the following code:

        public static System.Drawing.Bitmap Image1
        {
            get
            {
                return (System.Drawing.Bitmap)ResourceManager.GetObject("Image1");
            }
        }

So, it should work correctly for a WinForms project.

So, it does not use the localization parameter for them as well?

ResourceManager.GetObject uses the current thread culture (CultureInfo.CurrentUICulture)