Bagikan melalui


ResourceWriter.Generate Metode

Definisi

Menyimpan semua sumber daya ke aliran output dalam format default sistem.

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 ()

Penerapan

Pengecualian

Terjadi kesalahan I/O.

Terjadi kesalahan selama serialisasi objek.

Ini ResourceWriter telah ditutup dan tabel hash-nya tidak tersedia.

Contoh

Contoh kode berikut menggunakan Generate metode untuk menulis semua objek sumber daya di ResourceWriter kelas ke aliran output

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

Keterangan

Metode Generate ini dipanggil secara implisit oleh Close metode jika tidak dipanggil oleh kode aplikasi Anda.

Generate hanya dapat dipanggil sekali, setelah semua panggilan ke AddResource dan AddResourceData telah dilakukan. Jika pengecualian terjadi saat menulis sumber daya, aliran output akan ditutup untuk mencegah penulisan informasi yang tidak valid.

Generate tidak menutup aliran output dalam kasus normal. Kecuali Anda menggabungkan data tambahan dengan file .resources Anda atau memerlukan akses ke aliran setelahnya, Anda harus menelepon Close setelah memanggil Generate, atau cukup memanggil Close.

Berlaku untuk