Edit

Share via


StronglyTypedResourceBuilder Class

Definition

Provides support for strongly typed resources. This class cannot be inherited.

public ref class StronglyTypedResourceBuilder abstract sealed
public static class StronglyTypedResourceBuilder
type StronglyTypedResourceBuilder = class
Public Class StronglyTypedResourceBuilder
Inheritance
StronglyTypedResourceBuilder

Examples

The following example generates a class named DemoResources that is written in C# or Visual Basic (depending on the example's source code). This class is in the DemoApp namespace and has properties that return the bitmap of a logo and the name of an application. The example calls a CreateResourceFile method to create the necessary .resw file and requires that a bitmap file named Logo.bmp be found in the example's current directory. The code example uses the following resource file, named 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

Your application code can then use the class as follows:

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

Remarks

Typically, resources separate code from content within an application. Creating and consuming these resources makes it easier to develop localizable applications. In the .NET Framework, resources are usually consumed by using the ResourceManager class, which contains methods that provide access to culture-specific resources at run time. For more information about creating and consuming resources, see Resources in Desktop Apps.

Strongly typed resource support is a compile-time feature that encapsulates access to resources by creating classes that contain a set of static, read-only (get) properties. This provides an alternative way to consume resources instead of calling the ResourceManager.GetString and ResourceManager.GetObject methods.

The basic functionality for strongly typed resource support is provided by the StronglyTypedResourceBuilder class (as well as the /str command-line option in the Resgen.exe (Resource File Generator)). The output of the Create method is a class that contains strongly typed properties that match the resources that are referenced in the input parameter. This class provides read-only access to the resources that are available in the file processed.

Methods

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

Generates a class file that contains strongly typed properties that match the resources referenced in the specified collection.

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

Generates a class file that contains strongly typed properties that match the resources referenced in the specified collection.

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

Generates a class file that contains strongly typed properties that match the resources in the specified .resx file.

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

Generates a class file that contains strongly typed properties that match the resources in the specified .resx file.

VerifyResourceName(String, CodeDomProvider)

Generates a valid resource string based on the specified input string and code provider.

Applies to