Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
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)

'Declaration
Public NotInheritable 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 Applications.

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:

Imports Microsoft.VisualBasic
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:

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

.NET Framework

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

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:
© 2015 Microsoft