Export (0) Print
Expand All

ResourceWriter.AddResource Method (String, Object)

Adds a named resource specified as an object to the list of resources to be written.

Namespace:  System.Resources
Assembly:  mscorlib (in mscorlib.dll)

public void AddResource(
	string name,
	Object value
)

Parameters

name
Type: System.String

The name of the resource.

value
Type: System.Object

The value of the resource.

Implements

IResourceWriter.AddResource(String, Object)

ExceptionCondition
ArgumentException

name (or a name that varies only by capitalization) has already been added to this ResourceWriter.

ArgumentNullException

The name parameter is null.

InvalidOperationException

This ResourceWriter has been closed and its hash table is unavailable.

value must be serializable.

The resource is not written until the Generate method is called.

You can retrieve the resources written by the AddResource(String, Object) method by calling the ResourceManager.GetObject method.

The following example uses the AddResource(String, Object) method to add object data to a binary resources file.

using System;
using System.Resources;

public class Example
{
   public static void Main()
   {
      DonorColumns columns = new DonorColumns("Emplyee #", "Name", 
                                              "Total Amount", "Last Donation Date",
                                              "Last Donation Amount");
      ResourceWriter resFile = new ResourceWriter(@".\UIResources.resources");
      resFile.AddResource("Title", "Corporate Gold Star Donors");
      resFile.AddResource("NColumns", 5);
      resFile.AddResource("AppDate", new DateTime(2011, 5, 28));
      resFile.AddResource("AppVersion", new Version(1, 0, 217));
      resFile.AddResource("HRVersion", true);
      resFile.Generate();
      resFile.Close();               
   }
}

// Class to hold potentially localized column names.
[Serializable] public class DonorColumns
{
   readonly string ID;
   readonly string Name;
   readonly string Total;
   readonly string Last;
   readonly string Amt;

   public DonorColumns(string id, string name, string total, 
                  string last, string amt)
   {                  
      this.ID = id;
      this.Name = name;
      this.Total = total;
      this.Last = last;
      this.Amt = amt;                        
   }   
}

DonorColumns is a custom class whose fields contain the names of columns to be displayed in the user interface. Note that the class is marked with the SerializableAttribute attribute. Ordinarily, the class would be defined in a separate assembly, and a reference to it would be provided to the compiler at compile time.

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

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.

Show:
© 2014 Microsoft