ResXResourceWriter 类

定义

将资源写入 XML 资源 (.resx) 文件或输出流。Writes resources in an XML resource (.resx) file or an output stream.

public ref class ResXResourceWriter : System::Resources::IResourceWriter
public class ResXResourceWriter : System.Resources.IResourceWriter
type ResXResourceWriter = class
    interface IResourceWriter
    interface IDisposable
Public Class ResXResourceWriter
Implements IResourceWriter
继承
ResXResourceWriter
实现

示例

下面的示例创建一个名为CarResources.resx的 .resx 文件, 该文件存储了六个字符串、一个图标和两个应用程序定义的对象 (两个Automobile对象)。The following example creates a .resx file named CarResources.resx that stores six strings, an icon, and two application-defined objects (two Automobile objects). 请注意,本示例中定义并实例化的 Automobile 类使用 SerializableAttribute 属性进行标记。Note that the Automobile class, which is defined and instantiated in the example, is tagged with the SerializableAttribute attribute.

using System;
using System.Drawing;
using System.Resources;

[Serializable()] public class Automobile
{
   private string carMake;
   private string carModel;
   private int carYear;
   private int carDoors;
   private int carCylinders;
   
   public Automobile(string make, string model, int year) :  
                     this(make, model, year, 0, 0)   
   { }
   
   public Automobile(string make, string model, int year, 
                     int doors, int cylinders)
   {                     
      this.carMake = make;
      this.carModel = model;
      this.carYear = year;
      this.carDoors = doors;
      this.carCylinders = cylinders;
   }

   public string Make {
      get { return this.carMake; }
   }       
   
   public string Model {
      get {return this.carModel; }
   }       
   
   public int Year {
      get { return this.carYear; }
   }       
   
   public int Doors {
      get { return this.carDoors; }
   }       
   
   public int Cylinders {
      get { return this.carCylinders; }
   }       
}

public class Example
{
   public static void Main()
   {
      // Instantiate an Automobile object.
      Automobile car1 = new Automobile("Ford", "Model N", 1906, 0, 4);
      Automobile car2 = new Automobile("Ford", "Model T", 1909, 2, 4);
      // Define a resource file named CarResources.resx.
      using (ResXResourceWriter resx = new ResXResourceWriter(@".\CarResources.resx"))
      {
         resx.AddResource("Title", "Classic American Cars");
         resx.AddResource("HeaderString1", "Make");
         resx.AddResource("HeaderString2", "Model");
         resx.AddResource("HeaderString3", "Year");
         resx.AddResource("HeaderString4", "Doors");
         resx.AddResource("HeaderString5", "Cylinders");
         resx.AddResource("Information", SystemIcons.Information); 
         resx.AddResource("EarlyAuto1", car1);  
         resx.AddResource("EarlyAuto2", car2);  
      }
   }
}
Imports System.Drawing
Imports System.Resources

<Serializable()> Public Class Automobile
   Private carMake As String
   Private carModel As String
   Private carYear As Integer
   Private carDoors AS Integer
   Private carCylinders As Integer
   
   Public Sub New(make As String, model As String, year As Integer) 
      Me.New(make, model, year, 0, 0)   
   End Sub
   
   Public Sub New(make As String, model As String, year As Integer, 
                  doors As Integer, cylinders As Integer)
      Me.carMake = make
      Me.carModel = model
      Me.carYear = year
      Me.carDoors = doors
      Me.carCylinders = cylinders
   End Sub

   Public ReadOnly Property Make As String
      Get
         Return Me.carMake
      End Get   
   End Property       
   
   Public ReadOnly Property Model As String
      Get
         Return Me.carModel
      End Get   
   End Property       
   
   Public ReadOnly Property Year As Integer
      Get
         Return Me.carYear
      End Get   
   End Property       
   
   Public ReadOnly Property Doors As Integer
      Get
         Return Me.carDoors
      End Get   
   End Property       
   
   Public ReadOnly Property Cylinders As Integer
      Get
         Return Me.carCylinders
      End Get   
   End Property       
End Class

Module Example
   Public Sub Main()
      ' Instantiate an Automobile object.
      Dim car1 As New Automobile("Ford", "Model N", 1906, 0, 4)
      Dim car2 As New Automobile("Ford", "Model T", 1909, 2, 4)
      ' Define a resource file named CarResources.resx.
      Using resx As New ResXResourceWriter(".\CarResources.resx")
         resx.AddResource("Title", "Classic American Cars")
         resx.AddResource("HeaderString1", "Make")
         resx.AddResource("HeaderString2", "Model")
         resx.AddResource("HeaderString3", "Year")
         resx.AddResource("HeaderString4", "Doors")
         resx.AddResource("HeaderString5", "Cylinders")
         resx.AddResource("Information", SystemIcons.Information) 
         resx.AddResource("EarlyAuto1", car1)
         resx.AddResource("EarlyAuto2", car2)  
      End Using
   End Sub
End Module

注解

写入ResXResourceWriter XML 格式的资源。The ResXResourceWriter writes resources in XML format. 若要编写二进制资源文件, 请ResourceWriter使用。To write a binary resource file, use ResourceWriter.

使用AddResource方法将资源指定为名称/值对。Resources are specified as name/value pairs using the AddResource method. 资源名称在用于查找时区分大小写;不过, 为了更轻松地支持创作工具并帮助消除ResXResourceWriter bug, 不允许 .resx 文件具有仅大小写不同的名称。Resource names are case-sensitive when used for lookups; but to more easily support authoring tools and help eliminate bugs, ResXResourceWriter does not allow a.resx file to have names that vary only by case.

若要创建 .resx 文件, 请创建一个ResXResourceWriter具有唯一文件名的, 再调用AddResource一次, Generate然后调用将资源文件写入磁盘, 然后调用Close来关闭文件。To create a.resx file, create a ResXResourceWriter with a unique file name, call AddResource at least once, call Generate to write the resources file to disk, and then call Close to close the file. 如果Close需要, Generate调用将隐式地执行文件。Calling Close will implicitly Generate the file if required.

不一定按添加资源的顺序来写入资源。The resources are not necessarily written in the same order they were added.

有关 .resx 文件格式的详细信息, 请参阅以编程方式使用 .Resx 文件For more information on the format of a.resx file, see Working with .resx Files Programmatically.

备注

此类包含应用于所有成员的类级别的链接要求和继承要求。This class contains a link demand and an inheritance demand at the class level that applies to all members. 当直接调用方或派生类不具有完全信任权限时,将引发。SecurityExceptionA SecurityException is thrown when either the immediate caller or the derived class does not have full-trust permission.

构造函数

ResXResourceWriter(Stream)

初始化 ResXResourceWriter 类的新实例,它将资源写入到指定的流对象中。Initializes a new instance of the ResXResourceWriter class that writes the resources to the specified stream object.

ResXResourceWriter(Stream, Func<Type,String>)

初始化 ResXResourceWriter 类的新实例,它将资源写入指定的流对象并设置转换器委托。Initializes a new instance of the ResXResourceWriter class that writes the resources to a specified stream object and sets a converter delegate. 通过此委托,可以使用限定的程序集名称以面向 .NET Framework 4.NET Framework 4 之前的 .NET Framework 版本编写资源程序集。This delegate enables resource assemblies to be written that target versions of the .NET Framework before the .NET Framework 4.NET Framework 4 by using qualified assembly names.

ResXResourceWriter(String)

初始化 ResXResourceWriter 类的新实例,它将资源写入到指定文件中。Initializes a new instance of the ResXResourceWriter class that writes the resources to the specified file.

ResXResourceWriter(String, Func<Type,String>)

初始化 ResXResourceWriter 类的新实例,它将资源写入指定的文件并设置一个委托,通过该委托可以使用限定的程序集名称以面向 .NET Framework 4.NET Framework 4 之前的 .NET Framework 版本编写资源程序集。Initializes a new instance of the ResXResourceWriter class that writes the resources to a specified file and sets a delegate that enables resource assemblies to be written that target versions of the .NET Framework before the .NET Framework 4.NET Framework 4 by using qualified assembly names.

ResXResourceWriter(TextWriter)

初始化 ResXResourceWriter 类的新实例,它写入到指定的 TextWriter 对象中。Initializes a new instance of the ResXResourceWriter class that writes to the specified TextWriter object.

ResXResourceWriter(TextWriter, Func<Type,String>)

初始化 ResXResourceWriter 类的新实例,它将资源写入指定的 TextWriter 对象并设置一个委托,通过该委托可以使用限定的程序集名称以面向 .NET Framework 4.NET Framework 4 之前的 .NET Framework 版本编写资源程序集。Initializes a new instance of the ResXResourceWriter class that writes the resources to a specified TextWriter object and sets a delegate that enables resource assemblies to be written that target versions of the .NET Framework before the .NET Framework 4.NET Framework 4 by using qualified assembly names.

字段

BinSerializedObjectMimeType

指定二进制对象的默认内容类型。Specifies the default content type for a binary object. 此字段为只读。This field is read-only.

ByteArraySerializedObjectMimeType

指定字节数组对象的默认内容类型。Specifies the default content type for a byte array object. 此字段为只读。This field is read-only.

DefaultSerializedObjectMimeType

指定对象的默认内容类型。Specifies the default content type for an object. 此字段为只读。This field is read-only.

ResMimeType

指定 XML 资源的内容类型。Specifies the content type of an XML resource. 此字段为只读。This field is read-only.

ResourceSchema

指定写入 XML 文件时使用的架构。Specifies the schema to use in writing the XML file. 此字段为只读。This field is read-only.

SoapSerializedObjectMimeType

指定 SOAP 对象的内容类型。Specifies the content type for a SOAP object. 此字段为只读。This field is read-only.

Version

指定 XML 输出所符合的架构的版本。Specifies the version of the schema that the XML output conforms to. 此字段为只读。This field is read-only.

属性

BasePath

获取或设置在 ResXFileRef 对象中指定的相对文件路径的基路径。Gets or sets the base path for the relative file path specified in a ResXFileRef object.

方法

AddAlias(String, AssemblyName)

将指定别名添加到别名列表中。Adds the specified alias to a list of aliases.

AddMetadata(String, Byte[])

向要写入的资源的列表中添加值指定为字节数组的设计时属性。Adds a design-time property whose value is specified as a byte array to the list of resources to write.

AddMetadata(String, Object)

向要写入的资源的列表中添加值指定为对象的设计时属性。Adds a design-time property whose value is specified as an object to the list of resources to write.

AddMetadata(String, String)

向要写入的资源的列表中添加值指定为字符串的设计时属性。Adds a design-time property whose value is specified as a string to the list of resources to write.

AddResource(ResXDataNode)

ResXDataNode 对象中指定的已命名资源添加到要写入的资源列表中。Adds a named resource specified in a ResXDataNode object to the list of resources to write.

AddResource(String, Byte[])

将指定为字节数组的已命名资源添加到要写入的资源列表中。Adds a named resource specified as a byte array to the list of resources to write.

AddResource(String, Object)

将指定为对象的已命名资源添加到要写入的资源列表中。Adds a named resource specified as an object to the list of resources to write.

AddResource(String, String)

向资源添加字符串资源。Adds a string resource to the resources.

Close()

释放由 ResXResourceWriter 使用的所有资源。Releases all resources used by the ResXResourceWriter.

Dispose()

释放由 ResXResourceWriter 使用的所有资源。Releases all resources used by the ResXResourceWriter.

Dispose(Boolean)

释放由 ResXResourceWriter 占用的非托管资源,还可以另外再释放托管资源。Releases the unmanaged resources used by the ResXResourceWriter and optionally releases the managed resources.

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
Finalize()

此成员重写 Finalize() 方法。This member overrides the Finalize() method.

Generate()

将所有由 AddResource(String, Byte[]) 方法添加的资源写入到输出文件或流中。Writes all resources added by the AddResource(String, Byte[]) method to the output file or stream.

GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)

安全性

LinkDemand
完全信任直接调用方。for full trust for the immediate caller. 此成员不能由部分信任的代码使用。This member cannot be used by partially trusted code.

InheritanceDemand
完全信任继承者。for full trust for inheritors. 部分受信任的代码不能继承此成员。This member cannot be inherited by partially trusted code.

适用于

另请参阅