Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resources per version

Tags:

c#

resources

I know it is possible to store localized versions of resources to be retrieved by the application.

I would like to use a similar principle and to store different versions of data into a resource file/assembly, then use the version as the key to retrieve this data. Is this possible? Is this a common technique?

The data I will be storing is UI Control data identifications.

So for example I would have multiple resources available per version:

Version 1.0    btnID = "thisButton1"  
Version 2.0    btnID = "thisButton2"

The application would determine automatically which resource to pick up based on the version currently being used.

like image 445
lysergic-acid Avatar asked Dec 04 '11 17:12

lysergic-acid


1 Answers

You can create a custom class that will wrap the access to the resources and load the correct resource file internally. Assuming you have chosen to name your resource file Resources.1.0.0.0.resources, you can do the following:

  public class ResourceReader
  {
      private static ResourceManager _resourceManager = new ResourceManager(
           "Resources." + System.Reflection.Assembly
               .GetExecutingAssembly()
               .GetName()
               .Version.ToString(),
           Assembly.GetExecutingAssembly());

      // If you have resource property btnID, you can expose it like this:
      public static string BtnID
      {
          get
          {
              return _resourceManager.GetString("btnID");
          }
      }

      // you can add other properties for every value in the resource file.
  }

All you need to do is to know the exact version of your application and to ensure such resource file exists. This can be cumbersome if you have enabled automatic versioning in the assembly info (by using 1.0.* for instance). Of course, you may choose not to use the entire version, but only the major or major and minor version numbers.

like image 74
Ivaylo Slavov Avatar answered Oct 07 '22 07:10

Ivaylo Slavov