可以編寫一個使用反射來確定表單/用戶控件是否已被標記爲可本地化的單元測試。具體而言,如果某個類型已被標記爲可本地化,則會有一個嵌入的資源文件與該類型關聯,並且該文件將包含「>> $ this.Name」值。這裏有一些示例代碼:
private void CheckLocalizability()
{
try
{
Assembly activeAssembly = Assembly.GetAssembly(this.GetType());
Type[] types = activeAssembly.GetTypes();
foreach (Type type in types)
{
if (TypeIsInheritedFrom(type, "UserControl") || TypeIsInheritedFrom(type, "Form"))
{
bool localizable = false;
System.IO.Stream resourceStream = activeAssembly.GetManifestResourceStream(string.Format("{0}.resources", type.FullName));
if (resourceStream != null)
{
System.Resources.ResourceReader resourceReader = new System.Resources.ResourceReader(resourceStream);
foreach (DictionaryEntry dictionaryEntry in resourceReader)
{
if (dictionaryEntry.Key.ToString().Equals(">>$this.Name", StringComparison.InvariantCultureIgnoreCase))
{
localizable = true;
break;
}
}
}
if (!localizable)
{
Debug.Assert(false, string.Format("{0} is not marked localizable.", type.FullName));
}
}
}
}
catch (Exception ex)
{
Debug.Assert(false, string.Format("Exception occurred: Unable to check localization settings. {0}", ex.Message));
}
}
private bool TypeIsInheritedFrom(Type type, string baseType)
{
while (type != null)
{
if (type.Name.Equals(baseType, StringComparison.InvariantCultureIgnoreCase))
return true;
type = type.BaseType;
}
return false;
}
請讓我知道這是否有幫助。
對不起,在OP中不清楚。模板是這樣做的一種方式,但是,他們沒有執行本地化屬性需要爲真的策略。 – AxelEckenberger
Hmya,政策執行的選項在這裏非常有限。 Visual Studio讓程序員負責,而不是老闆。您可以編輯標準模板。 –