ResourceManager 覆盖 GetResourceFileName

发布于 2024-09-13 01:48:14 字数 1370 浏览 5 评论 0原文

我想重写 mscorlib v4 中 System.Resources.ResourceManager 类中的方法。

我想像这样重写 GetResourceFileName 方法;

    protected override string GetResourceFileName(CultureInfo culture) {
        string resourceFileName = base.GetResourceFileName(culture);
        return resourceFileName.Replace(".resources", ".resx");
    }

问题是,要实例化 ResourceManager 类,我必须使用静态方法 CreateFileBasedResourceManager,它返回 ResourceManager 的新实例。 (因为我需要基于文件的资源管理器)

我如何覆盖这个方法?我还尝试调用私有构造函数 CreateFileBasedResourceManager 调用来创建对象,但我收到此异常(无法使用反射调用私有构造函数?);

    [SecurityException: Request failed.]
  System.RuntimeMethodHandle.PerformSecurityCheck(Object obj, RuntimeMethodHandleInternal method, RuntimeType parent, UInt32 invocationFlags) +0
   System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks) +323
   System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +38
   System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) +35
   System.Xml.Xsl.XsltOld.FuncExtension.Invoke(XsltContext xsltContext, Object[] args, XPathNavigator docContext) +164
   MS.Internal.Xml.XPath.FunctionQuery.Evaluate(XPathNodeIterator nodeIterator) +430

I want to override a method in the System.Resources.ResourceManager class in mscorlib v4.

I want to override the method GetResourceFileName like this;

    protected override string GetResourceFileName(CultureInfo culture) {
        string resourceFileName = base.GetResourceFileName(culture);
        return resourceFileName.Replace(".resources", ".resx");
    }

The problem is, to instanciate a ResourceManager class I must use the static method CreateFileBasedResourceManager, which returns a new instance of the ResourceManager. (because I need the file based resourcemanager)

How can I override this method? I also tried to call the private constructor CreateFileBasedResourceManager calls to create the object, but i'm getting this exception (can't invoke private constructor with reflection?);

    [SecurityException: Request failed.]
  System.RuntimeMethodHandle.PerformSecurityCheck(Object obj, RuntimeMethodHandleInternal method, RuntimeType parent, UInt32 invocationFlags) +0
   System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks) +323
   System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +38
   System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) +35
   System.Xml.Xsl.XsltOld.FuncExtension.Invoke(XsltContext xsltContext, Object[] args, XPathNavigator docContext) +164
   MS.Internal.Xml.XPath.FunctionQuery.Evaluate(XPathNodeIterator nodeIterator) +430

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╄→承喏 2024-09-20 01:48:14

创建您自己的资源提供程序并在您的 app.config 或 web.config 中引用它。可以在此处找到示例

Create your own resource provider and reference it in your app.config or web.config. An example can be found here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文