語言

StronglyTypedResourceBuilder 類別

定義

提供強型別資源的支援。 此類別無法獲得繼承。

public ref class StronglyTypedResourceBuilder abstract sealed
public static class StronglyTypedResourceBuilder
type StronglyTypedResourceBuilder = class
Public Class StronglyTypedResourceBuilder
繼承
StronglyTypedResourceBuilder

範例

以下範例產生一個名為 DemoResources 的類別,該類別以 C# 或 Visual Basic(視範例的原始碼而定)撰寫。 這個類別位於命名空間中 DemoApp ,擁有回傳標誌位圖和應用程式名稱的屬性。 範例中呼叫一個 CreateResourceFile 方法來建立所需的 .resw 檔案,並要求在範例目前目錄中找到名為 Logo.bmp 的位圖檔案。 程式碼範例使用以下資源檔案,名為 demo.resx:

using Microsoft.CSharp;
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Drawing;
using System.IO;
using System.Resources;
using System.Resources.Tools;

public class Example
{
   public static void Main()
   {
      CreateResXFile();
      
      StreamWriter sw = new StreamWriter(@".\DemoResources.cs");
      string[] errors = null;
      CSharpCodeProvider provider = new CSharpCodeProvider();
      CodeCompileUnit code = StronglyTypedResourceBuilder.Create("Demo.resx", "DemoResources", 
                                                                 "DemoApp", provider, 
                                                                 false, out errors);    
      if (errors.Length > 0)
         foreach (var error in errors)
            Console.WriteLine(error); 

      provider.GenerateCodeFromCompileUnit(code, sw, new CodeGeneratorOptions());                                         
      sw.Close();
   }

   private static void CreateResXFile()
   {
      Bitmap logo = new Bitmap(@".\Logo.bmp");

      ResXResourceWriter rw = new ResXResourceWriter(@".\Demo.resx");
      rw.AddResource("Logo", logo); 
      rw.AddResource("AppTitle", "Demo Application");
      rw.Generate();
      rw.Close();
   }
}
Imports System.CodeDom
Imports System.CodeDom.Compiler
Imports System.Drawing
Imports System.IO
Imports System.Resources
Imports System.Resources.Tools

Module Example
   Public Sub Main()
      CreateResXFile()
      
      Dim sw As New StreamWriter(".\DemoResources.vb")
      Dim errors() As String = Nothing
      Dim provider As New VBCodeProvider()
      Dim code As CodeCompileUnit = StronglyTypedResourceBuilder.Create("Demo.resx", "DemoResources", 
                                                                        "DemoApp", provider, 
                                                                        false, errors)    
      If errors.Length > 0 Then
         For Each [error] In errors
            Console.WriteLine([error]) 
         Next
      End If
      provider.GenerateCodeFromCompileUnit(code, sw, New CodeGeneratorOptions())                                         
      sw.Close()
   End Sub
   
   Private Sub CreateResXFile()
      Dim logo As New Bitmap(".\Logo.bmp")

      Dim rw As New ResXResourceWriter(".\Demo.resx")
      rw.AddResource("Logo", logo) 
      rw.AddResource("AppTitle", "Demo Application")
      rw.Generate()
      rw.Close()
   End Sub
End Module

您的應用程式代碼可以下方式使用該類別:

this.Text = DemoApp.DemoResources.AppTitle;
System.Drawing.Bitmap bmp = DemoApp.DemoResources.Logo;
Me.Text = DemoApp.DemoResources.AppTitle
Dim bmp As System.Drawing.Bitmap = DemoApp.DemoResources.Logo

備註

通常,資源會在應用程式中將程式碼與內容分離。 建立並運用這些資源,使開發可本地化的應用程式變得更容易。 在 .NET 框架中,資源通常透過使用 ResourceManager 類別來消耗,該類別包含可在執行時提供特定文化資源存取的方法。 欲了解更多關於建立與使用資源的資訊,請參閱 桌面應用程式中的資源。

強型別資源支援是一種編譯時功能,透過建立包含一組靜態、唯讀(get)屬性的類別來封裝資源存取。 這提供了另一種消耗資源的方式,而非 ResourceManager.GetString 呼叫 and ResourceManager.GetObject 方法。

強型別資源支援的基本功能由 StronglyTypedResourceBuilder 類別提供(以及 /strResgen.exe 中的命令列選項 (資源檔案產生器))。 該 Create 方法的輸出是一個類別,包含強型別屬性,與輸入參數中引用的資源相符。 此類別提供對處理檔案中可用資源的唯讀存取。

方法

名稱 Description
Create(IDictionary, String, String, CodeDomProvider, Boolean, String[])

產生一個類別檔案,包含與指定集合中引用資源相符的強型別屬性。

Create(IDictionary, String, String, String, CodeDomProvider, Boolean, String[])

產生一個類別檔案,包含與指定集合中引用資源相符的強型別屬性。

Create(String, String, String, CodeDomProvider, Boolean, String[])

產生一個類別檔案,包含與指定 .resx 檔案中資源相符的強型別屬性。

Create(String, String, String, String, CodeDomProvider, Boolean, String[])

產生一個類別檔案,包含與指定 .resx 檔案中資源相符的強型別屬性。

VerifyResourceName(String, CodeDomProvider)

根據指定的輸入字串與程式碼提供者產生有效的資源字串。

適用於