5

Following the steps provided in this SO post, I have successfully added a resources file and can query the resources I've included, but I must manually use the ResourceManager class to get the embedded resources - like

let someText = ResourceManager("strings", System.Reflection.Assembly.GetCallingAssembly()).GetObject("SomeText")

In VB.NET and C#, these resources are strongly named. So if I have a resources FILE called "SomeText.txt", I can call it in code with

var someText = MyDefaultNamespace.Properties.Resources.SomeText;

Is there a way to make the F# resources strongly named, like they are in C# and VB.NET, in F#? A t4 text template, maybe?

Community
  • 1
  • 1
turkinator
  • 905
  • 9
  • 25
  • 1
    In your C# (or VB) project, right-click that `SomeText` and click "Go to Definition". See how it's done? – Fyodor Soikin Jan 12 '17 at 18:47
  • +1. This leads me to the Resources.Designer.cs file (who's properties include a Custom Tool `ResXFileCodeGenerator`), and it seems to have all resources included as their own static properties with get accessors. In F#, my Resources.resx does not have a Designer.fs file, and the Resources.resx file does not have a "Custom Tool" option. Ideas? – turkinator Jan 12 '17 at 19:05
  • Idea: write the `Resources.designer.fs` file by hand. – Fyodor Soikin Jan 12 '17 at 20:19
  • 4
    I think you want https://fsprojects.github.io/FSharp.Configuration/ResXProvider.html – CaringDev Jan 12 '17 at 22:47
  • Thanks, @CaringDev, that appears to be what I'm looking for, but it presents a whole new problem - it appears to only work for resource FILES if the file is in the output directory. I'm looking into the source to see if, at runtime, it can pull the resx file that is an embedded resources of my assembly. There's not a lot of chatter about that ResXProvider – turkinator Jan 13 '17 at 20:08
  • Additionally, I just noticed that I can't change access modifiers on resources files in my resx file. :-/ – turkinator Jan 13 '17 at 20:45
  • @epicTurkey the type provider *does* support embedded files, it's just not released yet. You could compile from source. And yes, you're right, that project is not in a very good shape... maybe you can help? – CaringDev Jan 14 '17 at 08:32
  • @TaterJuice, you can, either by using `PublicResXFileCodeGenerator` or in the menu of the RESX editor window, change the access modifier to `public`. See also https://stackoverflow.com/questions/4274311/visual-studio-resx-file-default-internal-to-public – Abel Oct 28 '19 at 12:50

1 Answers1

3

Although it's definitely a workaround, one could move resources into a C# or VB.NET project and change the access modifiers for the generated resource types to public so that you can access them from F# as described here. In short:

  • Create a C# project and add a resource (RESX) file
  • Change the custom tool from ResXFileCodeGenerator to PublicResXFileCodeGenerator
  • Or: change the access modifier to Public: Change access modifier for RESX file
Abel
  • 56,041
  • 24
  • 146
  • 247
jpierson
  • 16,435
  • 14
  • 105
  • 149