无法使用Environment.GetResourceString静态方法

发布于 2024-08-12 11:42:54 字数 467 浏览 5 评论 0原文

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            var x = Environment.GetResourceString("test"); //compile-time error
        }
    }
}

错误是:“System.Environment”不包含“GetResourceString”的定义。

编辑:OP 表示他正在使用 Compact Framework v3.5。

我看不懂图片,我的代码有什么问题吗?谢谢!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            var x = Environment.GetResourceString("test"); //compile-time error
        }
    }
}

The error is: 'System.Environment' does not contain a definition for 'GetResourceString'.

EDIT: OP has stated that he's using the Compact Framework, v3.5.

I dont get the picture, what's wrong with my code? Thanks!

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

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

发布评论

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

评论(3

葬シ愛 2024-08-19 11:42:54

Environment.GetResourceString 不是公开的,

internal static string GetResourceString(string key);

请参阅 Michael Petrotta 的回答以了解如何访问资源,或者查看此处的示例 http://msdn.microsoft.com/en-us/library/system.resources.resourcemanager.aspx

Environment.GetResourceString is not public

internal static string GetResourceString(string key);

See Michael Petrottas answer for how to access resources or have a look at the samples here http://msdn.microsoft.com/en-us/library/system.resources.resourcemanager.aspx

茶底世界 2024-08-19 11:42:54

根据 MSDN,仅适用于 Compact Framework 2.0 版本。如果这篇文章可信,那么它在标准框架中从未存在过。

你想做什么?如果您需要本地化,您可能需要 ResourceManager.GetString

System.Resources.ResourceManager myManager = new 
   System.Resources.ResourceManager("ResourceNamespace.myResources", 
   myAssembly);

// Retrieves String and Image resources.
string myString = myManager.GetString("StringResource");

Environment.GetResourceString appears to be present, according to MSDN, only in version 2.0 of the Compact Framework. If the article is to be believed, it's never existed in the standard framework.

What are you trying to do? If it's localization you're after, you may want ResourceManager.GetString.

System.Resources.ResourceManager myManager = new 
   System.Resources.ResourceManager("ResourceNamespace.myResources", 
   myAssembly);

// Retrieves String and Image resources.
string myString = myManager.GetString("StringResource");
永言不败 2024-08-19 11:42:54

您无法访问Environment.GetResourceString,但如果您需要访问mscorlib 的内部错误消息,请定义您自己的实现。

using System;
using System.Globalization;
using System.Reflection;
using System.Resources;

static class EnvironmentEx
{
    // Mscorlib's resources.
    private static ResourceSet resources = null;

    // Gets mscorlib's internal error message.
    public static string GetResourceString(string name)
    {
        if (resources == null)
        {
            var assembly = Assembly.GetAssembly(typeof(object));
            var assemblyName = assembly.GetName().Name;
            var manager = new ResourceManager(assemblyName, assembly);
            resources = manager.GetResourceSet(CultureInfo.CurrentUICulture, true, true);
        }
        return resources.GetString(name ?? throw new ArgumentNullException(nameof(name)));
    }

    // Gets parametrized mscorlib's internal error message.
    public static string GetResourceString(string name, params object[] args)
    {
        return string.Format(GetResourceString(name), args);
    }
    
    static void Main()
    {
        string message = GetResourceString("ArgumentOutOfRange_Bounds_Lower_Upper", -1, 1);
        // message = "Argument must be between -1 and 1".
    }
}

PS 这里列出了所有消息及其 ID。

You cannot access Environment.GetResourceString, but if you need access to mscorlib's internal error message, define your own implementation of that.

using System;
using System.Globalization;
using System.Reflection;
using System.Resources;

static class EnvironmentEx
{
    // Mscorlib's resources.
    private static ResourceSet resources = null;

    // Gets mscorlib's internal error message.
    public static string GetResourceString(string name)
    {
        if (resources == null)
        {
            var assembly = Assembly.GetAssembly(typeof(object));
            var assemblyName = assembly.GetName().Name;
            var manager = new ResourceManager(assemblyName, assembly);
            resources = manager.GetResourceSet(CultureInfo.CurrentUICulture, true, true);
        }
        return resources.GetString(name ?? throw new ArgumentNullException(nameof(name)));
    }

    // Gets parametrized mscorlib's internal error message.
    public static string GetResourceString(string name, params object[] args)
    {
        return string.Format(GetResourceString(name), args);
    }
    
    static void Main()
    {
        string message = GetResourceString("ArgumentOutOfRange_Bounds_Lower_Upper", -1, 1);
        // message = "Argument must be between -1 and 1".
    }
}

P. S. Here a list of all messages with their ID.

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