4
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。

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

4

3 回答 3

8

Environment.GetResourceString 不公开

internal static string GetResourceString(string key);

有关如何访问资源的信息,请参阅 Michael Petrottas 的回答或查看此处的示例http://msdn.microsoft.com/en-us/library/system.resources.resourcemanager.aspx

于 2009-11-25T01:54:51.770 回答
6

Environment.GetResourceString根据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");
于 2009-11-25T01:30:56.093 回答
1

您无法访问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 的列表。

于 2022-02-07T20:43:22.307 回答