【发布时间】:2020-03-09 16:54:16
【问题描述】:
有没有办法自动生成一个文件,可以对资源执行线程安全、文化特定的访问? Visual Studio 为我的 resx 文件生成的当前 Resource.Designer.cs 类如下所示:
private static global::System.Resources.ResourceManager resourceMan;
private static global::System.Globalization.CultureInfo resourceCulture;
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
internal Resource()
{
}
/// <summary>
/// Returns the cached ResourceManager instance used by this class.
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
public static global::System.Resources.ResourceManager ResourceManager
{
get
{
if (object.ReferenceEquals(resourceMan, null))
{
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("ServiceModels.Resource", typeof(Resource).Assembly);
resourceMan = temp;
}
return resourceMan;
}
}
/// <summary>
/// Overrides the current thread's CurrentUICulture property for all
/// resource lookups using this strongly typed resource class.
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
public static global::System.Globalization.CultureInfo Culture
{
get
{
return resourceCulture;
}
set
{
resourceCulture = value;
}
}
/// <summary>
/// Looks up a localized string similar to Hiii.
/// </summary>
public static string Hello
{
get
{
return ResourceManager.GetString("Hello", resourceCulture);
}
}
这允许安全访问“Hello”资源,但在使用不同文化的不同资源文件时,它似乎不是线程安全的。例如,使用此代码,您可以通过
设置资源Culture = CultureInfo.SomeCulture;
这将为使用 ResourceManager 的所有线程设置文化。有没有办法自动生成一个允许线程安全、文化特定访问资源的文件,还是我必须自己实现一个?
【问题讨论】: