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 Framework中,通常會使用 ResourceManager 類別來取用資源,其中包含在執行時間提供文化特性特定資源的存取權的方法。 如需建立和使用資源的詳細資訊,請參閱 傳統型應用程式中的資源

強型別資源支援是一種編譯時間功能,可藉由建立包含一組靜態、唯讀 () get 屬性的類別來封裝資源的存取權。 這提供替代方式來取用資源,而不是呼叫 ResourceManager.GetStringResourceManager.GetObject 方法。

強型別資源支援的基本功能是由 StronglyTypedResourceBuilder 類別 (和 /strResgen.exe ( 資源檔產生 器中的命令列選項所提供,) ) 。 方法的 Create 輸出是類別,其中包含符合輸入參數中所參考資源的強型別屬性。 這個類別提供已處理之檔案中可用資源的唯讀存取權。

方法

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)

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

適用於