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