Export (0) Print
Expand All

StronglyTypedResourceBuilder Class

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

System.Object
  System.Resources.Tools.StronglyTypedResourceBuilder

Namespace:  System.Resources.Tools
Assembly:  System.Design (in System.Design.dll)

public static class StronglyTypedResourceBuilder

The StronglyTypedResourceBuilder type exposes the following members.

  NameDescription
Public methodStatic memberCreate(IDictionary, String, String, CodeDomProvider, Boolean, String[])Generates a class file that contains strongly typed properties that match the resources referenced in the specified collection.
Public methodStatic memberCreate(String, String, String, CodeDomProvider, Boolean, String[])Generates a class file that contains strongly typed properties that match the resources in the specified .resx file.
Public methodStatic memberCreate(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.
Public methodStatic memberCreate(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.
Public methodStatic memberVerifyResourceNameGenerates a valid resource string based on the specified input string and code provider.
Top

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.

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

Your application code can then use the class as follows:

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

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft