Michał Kuliński Michał Kuliński - 1 month ago 32
ASP.NET (C#) Question

How to override default System.Resources.ResourceManager in Resources.Designer.cs?

I want to override

System.Resources.ResourceManager
from
Resources.Designer.cs
file to achieve custom ResourceManager.GetString(...) method functionality. Is this possible?

Answer

I believe you're asking two separate questions here. You can certainly override ResourceManager.GetString. You can't, however, use that override in the auto-generated Resource.Designer.cs code. To use it, you'll have to write your own Resource designer class.

public class MyResourceManager : System.Resources.ResourceManager
  {
    // override
    public override GetString(string name)
      {
        // custom code 
      }
  }

 public class MyResourceDesigner
   {
      // use your custom class with override
      private static MyResourceManager resourceManager;
      private static CultureInfo resourceCulture;

      public static MyResourceManager ResourceManager
         {
            if (object.ReferenceEquals(resourceManager, null))
               {
                  // Resource is just the name of the .resx file
                  // be sure to include relevant namespaces
                  var temp = new MyResourceManager(
                     "MyProject.Resource", 
                     typeof(MyResourceDesigner).Assembly);
                  resourceManager = temp;
               }

            return resourceManager;
         }

      public static CultureInfo Culture
      {
         get
         {
            return resourceCulture;
         }

         set
         {
            resourceCulture = value;
         }
      }

      // start adding strongly-typed objects
      public static string Foo
      {
         get
         {
            // use your override
            return ResourceManager.GetString("Foo", resourceCulture);
         }
      }
  }