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.

Exception Condition
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
Available since 1.1
Return to top
Show: