ResourceWriter.Generate 方法
定义
将所有资源以系统默认格式保存到输出流。Saves all resources to the output stream in the system default format.
public:
virtual void Generate();
public:
void Generate();
public void Generate ();
abstract member Generate : unit -> unit
override this.Generate : unit -> unit
member this.Generate : unit -> unit
Public Sub Generate ()
实现
例外
出现 I/O 错误。An I/O error occurred.
对象序列化期间出现错误。An error occurred during serialization of the object.
该 ResourceWriter 已关闭,并且哈希表不可用。This ResourceWriter has been closed and its hash table is unavailable.
示例
下面的代码示例使用 Generate 方法将类中的所有资源对象写入 ResourceWriter 到输出流中。The following code example uses the Generate method to write all resource objects in a ResourceWriter class to the output stream
using namespace System;
using namespace System::Resources;
using namespace System::IO;
int main()
{
// Create a file stream to encapsulate items.resources.
FileStream^ fs = gcnew FileStream( "items.resources",FileMode::OpenOrCreate,FileAccess::Write );
// Open a resource writer to write from the stream.
IResourceWriter^ writer = gcnew ResourceWriter( fs );
// Add resources to the resource writer.
writer->AddResource( "String 1", "First String" );
writer->AddResource( "String 2", "Second String" );
writer->AddResource( "String 3", "Third String" );
// Generate the resources, and close the writer.
writer->Generate();
writer->Close();
}
using System;
using System.Resources;
using System.IO;
public class WriteResources
{
public static void Main(string[] args)
{
// Create a file stream to encapsulate items.resources.
FileStream fs = new FileStream("items.resources",
FileMode.OpenOrCreate,FileAccess.Write);
// Open a resource writer to write from the stream.
IResourceWriter writer = new ResourceWriter(fs);
// Add resources to the resource writer.
writer.AddResource("String 1", "First String");
writer.AddResource("String 2", "Second String");
writer.AddResource("String 3", "Third String");
// Generate the resources, and close the writer.
writer.Generate();
writer.Close();
}
}
Imports System.Resources
Imports System.IO
Public Class WriteResources
Public Shared Sub Main(ByVal args() As String)
' Create a file stream to encapsulate items.resources.
Dim fs As New FileStream("items.resources", _
FileMode.OpenOrCreate, FileAccess.Write)
' Open a resource writer to write from the stream.
Dim writer = New ResourceWriter(fs)
' Add resources to the resource writer.
writer.AddResource("String 1", "First String")
writer.AddResource("String 2", "Second String")
writer.AddResource("String 3", "Third String")
' Generate the resources, and close the writer.
writer.Generate()
writer.Close()
End Sub
End Class
注解
Generate Close 如果你的应用程序代码未调用方法,方法将由方法隐式调用。The Generate method is called implicitly by the Close method if it is not called by your application code.
Generate 在对和的所有调用都完成后,只能调用一次 AddResource AddResourceData 。Generate can only be called once, after all calls to AddResource and AddResourceData have been made. 如果在写入资源时发生异常,则输出流将关闭以防止写入无效信息。If an exception occurs while writing the resources, the output stream will be closed to prevent writing invalid information.
Generate 在正常情况下不会关闭输出流。Generate does not close the output stream in normal cases. 除非你要将额外的数据与 .resources 文件合并,或者之后需要访问流,否则应在 Close 调用后调用 Generate ,或者只需调用 Close 。Unless you are combining extra data with your .resources file or need access to the stream afterwards, you should call Close after calling Generate, or simply call Close.