C# - 从程序集中获取资源字符串的最快方法

发布于 2024-12-28 17:50:13 字数 248 浏览 6 评论 0原文

我真的不知道/没有答案,知道如何使用 c# 程序集中的 resx 文件中的键查找资源值。(或者可能是我无知)。

什么是最快的代码,我可以使用资源文件中的键检索字符串值,该资源文件作为资源嵌入集会。我在资源文件中存储异常的友好消息,并希望在需要时使用它们。

是否存在用于此目的的静态类?

有没有成熟的开源项目可供我使用?

I really don't know/have the answer, knowledge to find a resource value using a key from a resx file in a assembly using c#.(or may be i am ignorant).

What would be the fastest code, way i can retrieve string values or values using a key from a resource file which is embedded as resource in a assembly. I am storing friendly messages for exceptions in the resource file and would like to use them when required.

Does a static class exist for this purpose?

Are there open source mature projects i can use for this?

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

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

发布评论

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

评论(7

自由范儿 2025-01-04 17:50:13

如果资源与代码位于同一程序集中,则将执行以下操作:

String resourceValue = MyAssemblyNameSpace.Properties.Resources.ResourceName

取自这个SO答案

If the resource is in the same assembly as the code, then the following will do:

String resourceValue = MyAssemblyNameSpace.Properties.Resources.ResourceName

Taken from this SO answer.

酒与心事 2025-01-04 17:50:13
Assembly assembly = this.GetType().Assembly;
ResourceManager resourceManager = new ResourceManager("Resources.Strings", assembly);
string myString = resourceManager.GetString("value");
Assembly assembly = this.GetType().Assembly;
ResourceManager resourceManager = new ResourceManager("Resources.Strings", assembly);
string myString = resourceManager.GetString("value");
甚是思念 2025-01-04 17:50:13
string val = Resources.ResourceManager.GetString("resource_name");

给定“resource_name”,您可以检索资源值。

string val = Resources.ResourceManager.GetString("resource_name");

Given "resource_name" you can retrieve resource value.

む无字情书 2025-01-04 17:50:13

您可以使用 ResourceManger 从程序集获取字符串值

从程序集获取资源

ResourceManager ResManager= new ResourceManager("yourResource", 
            Assembly.GetExecutingAssembly());
String strResourveValue = ResManager.GetString("YourStringKey");

you can use ResourceManger to get the string value from Assembly

Get Resource from Assembly

ResourceManager ResManager= new ResourceManager("yourResource", 
            Assembly.GetExecutingAssembly());
String strResourveValue = ResManager.GetString("YourStringKey");
影子的影子 2025-01-04 17:50:13
 var thread = System.Threading.Thread.CurrentThread.CurrentUICulture.Name;
            var culture = new CultureInfo(thread);
            var resourceManager = new ResourceManager(typeof(Resources.Resource));
            string value = resourceManager.GetString(name, culture);
 var thread = System.Threading.Thread.CurrentThread.CurrentUICulture.Name;
            var culture = new CultureInfo(thread);
            var resourceManager = new ResourceManager(typeof(Resources.Resource));
            string value = resourceManager.GetString(name, culture);
空城旧梦 2025-01-04 17:50:13

当我为 C# 类型类库的单元测试创​​建一个名为 UnitTests 的新项目时,我右键单击并添加了一个新资源。我将其命名为 UnitTestsResources。我向该资源添加了 2 个字符串。然后我就能够像这样方便地访问它们

UnitTestsResources.NoDeviceRequireMsg

我很好奇它是如何工作的,所以我提取了资源文件后面的代码,它是有意义的。 Visual Studio 使用静态访问器创建了一个内部类。对我来说它看起来像这样,

[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
internal class UnitTestsResources {

    //Some auto generated code

    /// <summary>
    ///   Looks up a localized string similar to OPOS Device is required for test.
    /// </summary>
    internal static string DeviceRequireMsg {
        get {
            return ResourceManager.GetString("DeviceRequireMsg", resourceCulture);
        }
    }

    /// <summary>
    ///   Looks up a localized string similar to OPOS Device must not be installed for test.
    /// </summary>
    internal static string NoDeviceRequireMsg {
        get {
            return ResourceManager.GetString("NoDeviceRequireMsg", resourceCulture);
        }
    }
}

因为它仅用于我的单元测试,我对此感到满意。希望它对其他人有帮助。

When I made a new project for my unit tests of type C# class library called UnitTests, I right clicked and Added a new Resource. I named that UnitTestsResources. I added 2 strings to that resource. I was then able to conveniently able to access them like this

UnitTestsResources.NoDeviceRequireMsg

I was curious how that worked so i pulled up the code behind the resource file and it makes sense. Visual Studio made a internal class with static accessors.. It looks like this for me

[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
internal class UnitTestsResources {

    //Some auto generated code

    /// <summary>
    ///   Looks up a localized string similar to OPOS Device is required for test.
    /// </summary>
    internal static string DeviceRequireMsg {
        get {
            return ResourceManager.GetString("DeviceRequireMsg", resourceCulture);
        }
    }

    /// <summary>
    ///   Looks up a localized string similar to OPOS Device must not be installed for test.
    /// </summary>
    internal static string NoDeviceRequireMsg {
        get {
            return ResourceManager.GetString("NoDeviceRequireMsg", resourceCulture);
        }
    }
}

Since it is only for my unit tests I am content with this. Hope it helps someone else.

江心雾 2025-01-04 17:50:13

为了改进 Herzbube 的答案,我将展示我如何实现这一点...

而不是为资源文件创建项目或文件夹,只需右键单击您的项目并执行添加 -> 即可。新项目,然后选择资源文件。打开字符串中的资源文件,另存为有用的名称,然后导航到要使用它们的 C# 位置,那么它就是:

String resourceValue = MyProjectName.MyResourceFileName.MyResourceRowItem;

如果是这样不起作用,请注意 resx 文件中的访问修饰符下拉列表。

To improve on Herzbube's answer I will show how I implemented this...

Rather than creating projects or folders for the resource file, just right click your project and do add -> new item, then choose resources file. Open the resources file stick in your strings, save as a useful name, and navigate over to C# where you want to use them, then it is just:

String resourceValue = MyProjectName.MyResourceFileName.MyResourceRowItem;

If that isnt working pay attention to the access modifier drop down when inside your resx file.

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