Editare

Partajați prin


ResourceWriter.AddResourceData(String, String, Byte[]) Method

Definition

Adds a unit of data as a resource to the list of resources to be written.

public:
 void AddResourceData(System::String ^ name, System::String ^ typeName, cli::array <System::Byte> ^ serializedData);
public void AddResourceData (string name, string typeName, byte[] serializedData);
member this.AddResourceData : string * string * byte[] -> unit
Public Sub AddResourceData (name As String, typeName As String, serializedData As Byte())

Parameters

name
String

A name that identifies the resource that contains the added data.

typeName
String

The type name of the added data.

serializedData
Byte[]

A byte array that contains the binary representation of the added data.

Exceptions

name, typeName, or serializedData is null.

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

The current ResourceWriter object is not initialized. The probable cause is that the ResourceWriter object is closed.

Examples

The following example uses the AddResourceData method to write two integer values to a .resources file, and then uses a ResourceReader object to retrieve them.

using System;
using System.Collections;
using System.Resources;

public class Example
{
   public static void Main()
   {
      ResourceWriter rw = new ResourceWriter(@".\TypeResources.resources");
      int n1 = 1032;
      rw.AddResourceData("Integer1", "ResourceTypeCode.Int32", BitConverter.GetBytes(n1));
      int n2 = 2064;       
      rw.AddResourceData("Integer2", "ResourceTypeCode.Int32", BitConverter.GetBytes(n2));
      rw.Generate();
      rw.Close();

      ResourceReader rr = new ResourceReader(@".\TypeResources.resources");
      IDictionaryEnumerator e = rr.GetEnumerator();
      while (e.MoveNext())
         Console.WriteLine("{0}: {1}", e.Key, e.Value);
   }
}
// The example displays the following output:
//       Integer2: 2064
//       Integer1: 1032
Imports System.Collections
Imports System.Resources

Module Example
   Public Sub Main()
      Dim rw As New ResourceWriter(".\TypeResources.resources")
      Dim n1 As Integer = 1032
      rw.AddResourceData("Integer1", "ResourceTypeCode.Int32", BitConverter.GetBytes(n1))
      Dim n2 As Integer = 2064       
      rw.AddResourceData("Integer2", "ResourceTypeCode.Int32", BitConverter.GetBytes(n2))
      rw.Generate()
      rw.Close()

      Dim rr As New ResourceReader(".\TypeResources.resources")
      Dim e As IDictionaryEnumerator = rr.GetEnumerator()
      Do While e.MoveNext()
         Console.WriteLine("{0}: {1}", e.Key, e.Value)
      Loop
    End Sub
End Module
' The example displays the following output:
'       Integer2: 2064
'       Integer1: 1032

Remarks

Use the AddResourceData method to add a resource in binary form (that is, as an array of bytes) to the list of resources to be written. You must specify the name of the resource, the type name of the data contained in the resource, and the binary representation of the data itself. After you have added each resource you require, use the Generate method to write the list of resources to the resources file or stream that was specified in the ResourceWriter constructor.

typeName is a string that represents the data type of the resource. It can be any of the following values:

  • The string representation of a ResourceTypeCode enumeration member that indicates the data type of the resource. ResourceTypeCode is a private enumeration that is used by Resgen.exe to indicate that a special binary format is used to store one of 19 common data types. These include the .NET Framework primitive data types (Boolean, Byte, Char, Decimal, Double, Int16, Int32, Int64, Single, SByte, UInt16, UInt32, UInt64), as well as String, DateTime, and TimeSpan. In addition, the ResourceTypeCode enumeration includes the values shown in the following table.

    ResourceTypeCode value Description
    ResourceTypeCode.ByteArray The data is a byte array.
    ResourceTypeCode.Null The data is a null reference.
    ResourceTypeCode.Stream The data is stored in a stream.
  • A string that contains the fully qualified name of the type whose binary data is assigned to the serializedData argument (for example, System.String). In addition, for types that are not part of the .NET Framework class library, the string includes the name, version, culture, and public key of the assembly that contains the type. For example, the following string indicates that the serialized data represents an instance of the Person type in the Extensions namespace, which is found in version 1.0 of an assembly named Utility that has no public key and no designated culture.

    Extensions.Person, Utility, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

A parallel method for reading resource data written with the AddResourceData method is ResourceReader.GetResourceData.

Applies to

See also