如何在Resources.Designer.cs中重写默认的System.Resources.ResourceManag

本文关键字:Resources 默认 System ResourceManag 重写 cs Designer | 更新日期: 2023-09-27 18:16:13

我想从Resources.Designer.cs文件重写System.Resources.ResourceManager以实现自定义ResourceManager.GetString(…)方法功能。这可能吗?

如何在Resources.Designer.cs中重写默认的System.Resources.ResourceManag

我创建了一个覆盖GetString()调用的CustomResourceManager,像这样:

public class CustomResourceManager : ResourceManager
{
    public CustomResourceManager(Type resourceSource)
        :base(resourceSource)
    {
    }
    public CustomResourceManager(string baseName, Assembly assembly)
        : base(baseName, assembly)
    {
    }
    public CustomResourceManager(string baseName, Assembly assembly, Type usingResourceSet)
        : base(baseName, assembly, usingResourceSet)
    {
    }
    public override string GetString(string name)
    {
        // your business logic
    }
    public override string GetString(string name, CultureInfo culture)
    {
        // your business logic
    }
}

然后我在我的项目中添加了一个预构建步骤,用生成的Resource.Designer.cs文件中的CustomResourceManager替换System.Resources.ResourceManager的创建:

powershell -command "(gc ..'Resources'Resource.Designer.cs).Replace('"new global::System.Resources.ResourceManager'", '"new CustomResourceManager'") | set-content ..'Resources'Resource.Designer.cs -Encoding UTF8"

我们走了!

我相信你在这里问了两个不同的问题。你当然可以重写ResourceManager.GetString。但是,您不能在自动生成的Resource.Designer.cs代码中使用该覆盖。要使用它,您必须编写自己的Resource设计器类。

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
  {
    get
    {
      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);
    }
  }
}

使用powershell命令替换ResourceManager的替代方法可以在这里找到:ResXFileCodeGenerator -重写输出(想要使用自定义ResourceManager)

在启动时使用反射来替换自动生成的静态资源类的resourceeman属性,如下所示:

var innerField = typeof(Resources.Resources).GetField("resourceMan",
    BindingFlags.NonPublic | BindingFlags.Static);
if (innerField != null)
{
    innerField.SetValue(null,
    new ExtendedResourceManager("MyNamespace.ResourceFileWithoutResx",
        typeof(Resources.Resources).Assembly));
}
innerField = typeof(Resources.Resources).GetField("resourceCulture",
    BindingFlags.NonPublic | BindingFlags.Static);
innerField.SetValue(null, CultureInfo.CurrentUICulture);