StronglyTypedResourceBuilder Class

Definition

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

C#
public static 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:

C#
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();
   }
}

Your application code can then use the class as follows:

C#
this.Text = DemoApp.DemoResources.AppTitle;
System.Drawing.Bitmap bmp = 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

Produkt Verzie
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 7, 8, 9, 10