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)

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

The .NET Framework version 2.0 supports strongly-typed resources. 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, rather than using the GetString and GetObject methods of the ResourceManager class.

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, after the resources are created, they 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 these processes, see Resources in Applications.

Strongly-typed resource support is an alternative to using the ResourceManager class to consume resources. The basic functionality is provided by the StronglyTypedResourceBuilder class (as well as a corresponding 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 strongly-typed, read-only access to the resources that are available in the file processed.

The following code example generates a class that is written in C#, is in the DemoApp namespace, is named DemoResources, and has properties that return the bitmap of a logo and the name of an application. The code example uses the following file, named demo.resx:

<data name="Logo" type="System.Drawing.Bitmap, System.Drawing, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base 64 encoded value omitted here]
</value>
</data>
<data name="MyAppTitle">
<value>Demo Application</value>
</data>

Call the Create method with the following arguments:

StronglyTypedResourceBuilder.Create("demo.resx", "DemoResources", "DemoApp", new CSharpCodeProvider(), false, out errors[]) 

The Create method generates the following code:

using System;
using System.IO;
using System.Resources;

namespace DemoApp
{
public class DemoResources
{
protected static ResourceManager rm = new ResourceManager(typeof(className));
public static System.Resources.ResourceManager resourceManager 
{ 
get { return rm; }
}
public static System.Drawing.Bitmap Logo
{
get { return (System.Drawing.Bitmap)rm.GetObject("logo"); }
}
    public static string MyAppTitle
    {
      get { return rm.GetString("MyAppTitle"); }
    }
}
}

In your application code, you can then use the class as follows:

this.Text = DemoResources.MyAppTitle;
System.Drawing.Bitmap bmp = DemoResources.Logo;

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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.

Community Additions

ADD
Show:
© 2015 Microsoft