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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8982366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 05:25:34  来源:igfitidea点击:

C# - Fastest way to get resource string from assembly

c#performanceresourcesassembliesresx

提问by Deeptechtons

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

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

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

什么是最快的代码,我可以从作为资源嵌入到程序集中的资源文件中检索string valuesvalues使用密钥的方式。我在资源文件中存储异常的友好消息,并希望在需要时使用它们。

Does a static class exist for this purpose?

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

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

是否有我可以使用的开源成熟项目?

采纳答案by sblom

Assembly assembly = this.GetType().Assembly;
ResourceManager resourceManager = new ResourceManager("Resources.Strings", assembly);
string myString = resourceManager.GetString("value");

回答by Ravi Gadag

you can use ResourceManger to get the string value from Assembly

您可以使用 ResourceManger 从 Assembly 中获取字符串值

Get Resource from Assembly

从程序集获取资源

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

回答by herzbube

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.

取自this SO answer

回答by Robert Snyder

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

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

UnitTestsResources.NoDeviceRequireMsg

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

我很好奇它是如何工作的,所以我提取了资源文件背后的代码,这是有道理的。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);
        }
    }
}

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

由于它仅用于我的单元测试,因此我对此感到满意。希望它可以帮助别人。

回答by Chris

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

为了改进 Herzbube 的回答,我将展示我是如何实现的......

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:

无需为资源文件创建项目或文件夹,只需右键单击您的项目并添加 -> 新项目,然后选择资源文件。打开你的字符串中的资源文件,保存为一个有用的名称,然后导航到你想要使用它们的 C#,然后它只是:

String resourceValue = MyProjectName.MyResourceFileName.MyResourceRowItem;

String resourceValue = MyProjectName.MyResourceFileName.MyResourceRowItem;

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

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

回答by SurenSaluka

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

Given "resource_name"you can retrieve resource value.

鉴于"resource_name"您可以检索资源值。

回答by Anup Shetty

 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);