ResourceManager 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化 ResourceManager 類別的新執行個體。
多載
| 名稱 | Description |
|---|---|
| ResourceManager() |
初始化一個以預設值的新類別實例 ResourceManager 。 |
| ResourceManager(Type) |
初始化一個新的類別實例 ResourceManager ,根據指定類型物件的資訊在衛星組件中查找資源。 |
| ResourceManager(String, Assembly) |
初始化一個新的類別實例,該實例 ResourceManager 在指定組合語言中查找包含在指定根名稱檔案中的資源。 |
| ResourceManager(String, Assembly, Type) |
初始化一個新的類別實例 ResourceManager ,使用指定的 ResourceSet 類別查詢該組合中具有指定根名稱的檔案中的資源。 |
ResourceManager()
初始化一個以預設值的新類別實例 ResourceManager 。
protected:
ResourceManager();
protected ResourceManager();
Protected Sub New ()
備註
這個建構子只有在你自己寫出由該 ResourceManager 類別衍生的類別時才有用。
適用於
ResourceManager(Type)
初始化一個新的類別實例 ResourceManager ,根據指定類型物件的資訊在衛星組件中查找資源。
public:
ResourceManager(Type ^ resourceSource);
public ResourceManager(Type resourceSource);
new System.Resources.ResourceManager : Type -> System.Resources.ResourceManager
Public Sub New (resourceSource As Type)
參數
- resourceSource
- Type
一種資源管理器用來推導所有尋找 .resources 檔案資訊的類型。
例外狀況
參數 resourceSource 為 null。
備註
桌面應用程式
在傳統型應用程式中,資源管理員會使用 resourceSource 參數來載入特定資源檔,如下所示:
-
NeutralResourcesLanguageAttribute如果屬性不是用來指出預設文化特性的資源位於附屬元件中,資源管理員會假設預設文化特性的資源檔位於與 參數所
resourceSource指定的類型相同的元件中。 - 資源管理員假設預設資源檔與 參數所
resourceSource指定的類型具有相同基底名稱。 - 資源管理員會使用預設的 ResourceSet 類別來操作資源檔案。
例如,假設類型為 MyCompany.MyProduct.MyType,資源管理員會在定義 的元件中尋找名為 MyCompany.MyProduct.MyType.resources 的 MyType 檔案。
在 Visual Studio 中,資源設計工具會自動產生程式代碼,其名稱與預設文化特性之 internal 檔案的基底名稱相同,以定義 Friend (在 C# 中) 或 [Visual Basic] 類別。 這可讓您具現化 ResourceManager 物件,並結合一組特定的資源,方法是取得名稱對應至資源名稱的類型對象,因為只要類別對編譯程式可見,資源也必須一樣。 例如,如果 .resources 檔案名為 Resource1,下列語句會具現化 ResourceManager 物件來管理名為 Resource1 的 .resources 檔案:
ResourceManager rm = new ResourceManager(typeof(Resource1));
如果您未使用 Visual Studio,您可以建立沒有成員的類別,其命名空間和名稱與預設 .resources 檔案的成員相同。 此範例提供圖例。
Example
下列範例會使用 建 ResourceManager(Type) 構函式來具現化 ResourceManager 物件。 它由英文(en)、法文(法國)(fr-FR)和俄羅斯(俄羅斯)(ru-RU)文化 .txt 檔案彙編的資源組成。 此範例會將目前的文化特性和目前的UI文化特性變更為英文(美國)、法文(法國)、俄羅斯文(俄羅斯)和瑞典文(瑞典)。 然後,它會呼叫 GetString(String) 方法來擷取本地化的字串,其會顯示視一天時間而定的問候語。
此範例需要三個以文字為基礎的資源檔,如下表所列。 每個檔案都包含名為 Morning、 Afternoon和 的 Evening字串資源。
| 文化特性 | 檔案名稱 | 資源名稱 | 資源值 |
|---|---|---|---|
| en-US | GreetingResources.txt | Morning |
早安 |
| en-US | GreetingResources.txt | Afternoon |
下午好 |
| en-US | GreetingResources.txt | Evening |
晚上好 |
| fr-FR | GreetingResources.fr-FR.txt | Morning |
你好 |
| fr-FR | GreetingResources.fr-FR.txt | Afternoon |
你好 |
| fr-FR | GreetingResources.fr-FR.txt | Evening |
Bonsoir |
| ru-RU | GreetingResources.ru-RU.txt | Morning |
Доброе утро |
| ru-RU | GreetingResources.ru-RU.txt | Afternoon |
Добрый день |
| ru-RU | GreetingResources.ru-RU.txt | Evening |
Добрый вечер |
您可以使用下列批處理文件來編譯 Visual Basic 範例,並建立名為 Greet.exe的可執行檔。 若要使用 C# 編譯,請將編譯程式名稱從 vbc 變更為 csc ,並將擴展名從 .vb 變更為 .cs。
resgen GreetingResources.txt
vbc Greet.vb /resource: GreetingResources.resources
md fr-FR
resgen GreetingResources.fr-FR.txt
al /out:fr-FR\Greet.resources.dll /culture:fr-FR /embed: GreetingResources.fr-FR.resources
md ru-RU
resgen GreetingResources.ru-RU.txt
al /out:ru-RU\Greet.resources.dll /culture:ru-RU /embed: GreetingResources.ru-RU.resources
以下是範例的原始碼(適用於 Visual Basic 版本的ShowDate.vb,或 C# 版本的程式代碼ShowDate.cs)。
using System;
using System.Resources;
using System.Globalization;
using System.Threading;
[assembly: NeutralResourcesLanguage("en")]
public class Example2
{
public static void Main()
{
string[] cultureNames = [ "en-US", "fr-FR", "ru-RU", "sv-SE" ];
DateTime noon = new DateTime(DateTime.Now.Year, DateTime.Now.Month,
DateTime.Now.Day, 12, 0, 0);
DateTime evening = new DateTime(DateTime.Now.Year, DateTime.Now.Month,
DateTime.Now.Day, 18, 0, 0);
ResourceManager rm = new ResourceManager(typeof(GreetingResources));
foreach (var cultureName in cultureNames)
{
Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(cultureName);
Console.WriteLine($"The current UI culture is {CultureInfo.CurrentUICulture.Name}");
if (DateTime.Now < noon)
Console.WriteLine($"{rm.GetString("Morning")}!");
else if (DateTime.Now < evening)
Console.WriteLine($"{rm.GetString("Afternoon")}!");
else
Console.WriteLine($"{rm.GetString("Evening")}!");
Console.WriteLine();
}
}
internal class GreetingResources
{
}
}
// The example displays output like the following:
// The current UI culture is en-US
// Good afternoon!
//
// The current UI culture is fr-FR
// Bonjour!
//
// The current UI culture is ru-RU
// Добрый день!
//
// The current UI culture is sv-SE
// Good afternoon!
Imports System.Resources
Imports System.Globalization
Imports System.Threading
<Assembly:NeutralResourcesLanguage("en")>
Module Example
Public Sub Main()
Dim cultureNames() As String = {"en-US", "fr-FR", "ru-RU", "sv-SE" }
Dim noon As New Date(Date.Now.Year, Date.Now.Month,
Date.Now.Day, 12,0,0)
Dim evening As New Date(Date.Now.Year, Date.Now.Month,
Date.Now.Day, 18, 0, 0)
Dim rm As New ResourceManager(GetType(GreetingResources))
For Each cultureName In cultureNames
Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(cultureName)
Console.WriteLine("The current UI culture is {0}",
CultureInfo.CurrentUICulture.Name)
If Date.Now < noon Then
Console.WriteLine("{0}!", rm.GetString("Morning"))
ElseIf Date.Now < evening Then
Console.WriteLine("{0}!", rm.GetString("Afternoon"))
Else
Console.WriteLine("{0}!", rm.GetString("Evening"))
End If
Console.WriteLine()
Next
End Sub
End Module
Friend Class GreetingResources
End Class
' The example displays output like the following:
' The current UI culture is en-US
' Good afternoon!
'
' The current UI culture is fr-FR
' Bonjour!
'
' The current UI culture is ru-RU
' Добрый день!
'
' The current UI culture is sv-SE
' Good afternoon!
除了定義名為 Example的應用程式類別之外,原始程式碼也會定義名稱為的內部類別,其名稱 GreetingResources與資源檔的基底名稱相同。 這可藉由呼叫 ResourceManager 建構函式,成功具現化 ResourceManager(Type) 物件。
請注意,輸出會顯示適當的當地語系化字串,但當前 UI 文化特性是瑞典文(瑞典地區)時,它會使用英文資源。 由於瑞典文語言資源無法使用,因此應用程式會改用屬性所 NeutralResourcesLanguageAttribute 定義的預設文化特性資源。
適用於
ResourceManager(String, Assembly)
初始化一個新的類別實例,該實例 ResourceManager 在指定組合語言中查找包含在指定根名稱檔案中的資源。
public:
ResourceManager(System::String ^ baseName, System::Reflection::Assembly ^ assembly);
public ResourceManager(string baseName, System.Reflection.Assembly assembly);
new System.Resources.ResourceManager : string * System.Reflection.Assembly -> System.Resources.ResourceManager
Public Sub New (baseName As String, assembly As Assembly)
參數
- baseName
- String
資源檔案的根名稱,不含副檔名,但包含任何完全限定的命名空間名稱。 例如,資源檔案 MyApplication.MyResource 的根名稱。en-US.resources 是 MyApplication.MyResource。
- assembly
- Assembly
資源的主要組裝。
例外狀況
baseName或assembly參數為 null。
備註
桌面應用程式
在傳統型應用程式中,個別文化特性特定的資源檔應該包含在附屬元件中,而預設文化特性的資源文件應該包含在主要元件中。 假設衛星組件包含該組件清單中指定的一種文化資源,並在需要時載入。
Note
若要直接從 .resources 檔案擷取資源,而不是從元件中擷取資源,您必須改為呼叫 CreateFileBasedResourceManager 方法來具現化 ResourceManager 物件。
如果在baseName中找不到由assembly識別的資源檔,則方法會實例化ResourceManager物件,但嘗試擷取特定資源時會拋出例外,通常是MissingManifestResourceException。 如需診斷例外狀況原因的相關信息,請參閱類別主題的
Example
下列範例使用簡單的非本地化 「Hello World」 應用程式來說明建 ResourceManager(String, Assembly) 構函式。 名為 ExampleResources.txt 之文字檔案的內容為 Greeting=Hello。 編譯應用程式時,資源會內嵌在主要應用程式元件中。
您可以在命令提示字元使用 資源檔案產生器 (ResGen.exe) 將文字檔案轉換成二進位資源檔,如下所示:
resgen ExampleResources.txt
下列範例提供可具現化 ResourceManager 物件的可執行程式碼、提示使用者輸入名稱,並顯示問候語。
using System;
using System.Reflection;
using System.Resources;
public class Example1
{
public static void Main()
{
// Retrieve the resource.
ResourceManager rm = new ResourceManager("ExampleResources",
typeof(Example).Assembly);
string greeting = rm.GetString("Greeting");
Console.Write("Enter your name: ");
string name = Console.ReadLine();
Console.WriteLine($"{greeting} {name}!");
}
}
// The example produces output similar to the following:
// Enter your name: John
// Hello John!
Imports System.Globalization
Imports System.Reflection
Imports System.Resources
Module Example1
Public Sub Main()
' Retrieve the resource.
Dim rm As New ResourceManager("ExampleResources",
GetType(Example).Assembly)
Dim greeting As String = rm.GetString("Greeting")
Console.Write("Enter your name: ")
Dim name As String = Console.ReadLine()
Console.WriteLine("{0} {1}!", greeting, name)
End Sub
End Module
' The example produces output similar to the following:
' Enter your name: John
' Hello John!
您可以使用 C# 中的下列命令來編譯它:
csc Example.cs /resource:ExampleResources.resources
此範例會擷取包含資源檔的元件參考,方法是將定義於該元件中的型別傳遞至 typeof 函式 (在 C# 中)或函 GetType 式 (在 Visual Basic 中),並擷取其 Type.Assembly 屬性的值。
給繼承者的注意事項
此建構器使用系統提供的 ResourceSet 實作。 要使用自訂資源檔案格式,你應該從 ResourceSet 類別衍生,覆寫 GetDefaultReader() and GetDefaultWriter() 方法,並將該類型傳給 ResourceManager(String, Assembly, Type) 建構子。 使用自訂 ResourceSet 工具對於控制資源快取政策或支援您自己的資源檔案格式很有用,但通常並非必要。
適用於
ResourceManager(String, Assembly, Type)
初始化一個新的類別實例 ResourceManager ,使用指定的 ResourceSet 類別查詢該組合中具有指定根名稱的檔案中的資源。
public:
ResourceManager(System::String ^ baseName, System::Reflection::Assembly ^ assembly, Type ^ usingResourceSet);
public ResourceManager(string baseName, System.Reflection.Assembly assembly, Type? usingResourceSet);
public ResourceManager(string baseName, System.Reflection.Assembly assembly, Type usingResourceSet);
new System.Resources.ResourceManager : string * System.Reflection.Assembly * Type -> System.Resources.ResourceManager
Public Sub New (baseName As String, assembly As Assembly, usingResourceSet As Type)
參數
- baseName
- String
資源檔案的根名稱,不含副檔名,但包含任何完全限定的命名空間名稱。 例如,資源檔案 MyApplication.MyResource 的根名稱。en-US.resources 是 MyApplication.MyResource。
- assembly
- Assembly
資源的主要組裝。
- usingResourceSet
- Type
使用習慣的類型 ResourceSet 。 若 null,則使用預設執行時 ResourceSet 物件。
例外狀況
usingResourceSet 不是 的 ResourceSet導出類。
baseName或assembly參數為 null。
備註
個別文化專屬的資源檔案應包含在衛星組件中,而預設文化的資源檔案則應包含在主組件中。 假設衛星組件包含該組件清單中指定的一種文化資源,並在需要時載入。
Note
若要直接從 .resources 檔案取得資源,而非從組合語言擷取,必須呼叫該 CreateFileBasedResourceManager 方法來實例化物件 ResourceManager 。
如果在baseName中找不到由assembly識別的資源檔,則方法會實例化ResourceManager物件,但嘗試擷取特定資源時會拋出例外,通常是MissingManifestResourceException。 如需診斷例外狀況原因的相關信息,請參閱類別主題的
Note
參數 usingResourceSet 用於支援你自己的資源格式,通常為 null。 這與僅取 a Type 的構造子不同。
給呼叫者的注意事項
這個建構器讓你可以指定實 ResourceSet 作方式。 如果你不想要特定的 ResourceSet 實作,但想使用自訂資源檔案格式,應該從 ResourceSet 類別衍生,覆寫 GetDefaultReader() and GetDefaultWriter() 方法,並將該型別傳給這個建構子。