次の方法で共有


AssemblyBuilder.AddResourceFile メソッド

既存のリソース ファイルをこのアセンブリに追加します。

オーバーロードの一覧

既存のリソース ファイルをこのアセンブリに追加します。

[Visual Basic] Overloads Public Sub AddResourceFile(String, String)

[C#] public void AddResourceFile(string, string);

[C++] public: void AddResourceFile(String*, String*);

[JScript] public function AddResourceFile(String, String);

既存のリソース ファイルをこのアセンブリに追加します。

[Visual Basic] Overloads Public Sub AddResourceFile(String, String, ResourceAttributes)

[C#] public void AddResourceFile(string, string, ResourceAttributes);

[C++] public: void AddResourceFile(String*, String*, ResourceAttributes);

[JScript] public function AddResourceFile(String, String, ResourceAttributes);

使用例

[Visual Basic, C#, C++] 次のコード例は、 AddResourceFile を使用して、動的に作成されたアセンブリにリソース ファイルを結び付ける方法を示しています。

[Visual Basic, C#, C++] メモ   ここでは、AddResourceFile のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。

 

Imports System
Imports System.IO
Imports System.Threading
Imports System.Reflection
Imports System.Reflection.Emit

 _

Class AsmBuilderGetFileDemo
   
   Private Shared myResourceFileName As String = "MyResource.txt"
   
   
   Private Shared Function CreateResourceFile() As FileInfo
      
      Dim f As New FileInfo(myResourceFileName)
      Dim sw As StreamWriter = f.CreateText()
      
      sw.WriteLine("Hello, world!")
      
      sw.Close()
      
      Return f

   End Function 'CreateResourceFile
    
   
   Private Shared Function BuildDynAssembly() As AssemblyBuilder
      
      Dim myAsmFileName As String = "MyAsm.dll"
      
      Dim myDomain As AppDomain = Thread.GetDomain()
      Dim myAsmName As New AssemblyName()
      myAsmName.Name = "MyDynamicAssembly"
      
      Dim myAsmBuilder As AssemblyBuilder = myDomain.DefineDynamicAssembly(myAsmName, _
                        AssemblyBuilderAccess.RunAndSave)
      
      myAsmBuilder.AddResourceFile("MyResource", myResourceFileName)
      
      ' To confirm that the resource file has been added to the manifest,
      ' we will save the assembly as MyAsm.dll. You can view the manifest
      ' and confirm the presence of the resource file by running 
      ' "ildasm MyAsm.dll" from the prompt in the directory where you executed
      ' the compiled code. 
      myAsmBuilder.Save(myAsmFileName)
      
      Return myAsmBuilder

   End Function 'BuildDynAssembly
    
   
   Public Shared Sub Main()
      
      Dim myResourceFS As FileStream = Nothing
      
      CreateResourceFile()
      
      Console.WriteLine("The contents of MyResource.txt, via GetFile:")
      
      Dim myAsm As AssemblyBuilder = BuildDynAssembly()
      
      Try

         myResourceFS = myAsm.GetFile(myResourceFileName)

      Catch nsException As NotSupportedException
     
     Console.WriteLine("---")
     Console.WriteLine("System.Reflection.Emit.AssemblyBuilder.GetFile is not supported " + _
                 "in this SDK build.")
     Console.WriteLine("The file data will now be retrieved directly, via a new FileStream.")
     Console.WriteLine("---")
     myResourceFS = New FileStream(myResourceFileName, FileMode.Open) 

      End Try
      
      Dim sr As New StreamReader(myResourceFS, System.Text.Encoding.ASCII)
      Console.WriteLine(sr.ReadToEnd())
      sr.Close()

   End Sub 'Main 

End Class 'AsmBuilderGetFileDemo


[C#] 

using System;
using System.IO;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;

class AsmBuilderGetFileDemo

{
   private static string myResourceFileName = "MyResource.txt";

   private static FileInfo CreateResourceFile() 
   {

         FileInfo f = new FileInfo(myResourceFileName); 
    StreamWriter sw = f.CreateText();

    sw.WriteLine("Hello, world!");

    sw.Close();

    return f;

   }

   private static AssemblyBuilder BuildDynAssembly()
   {

    string myAsmFileName = "MyAsm.dll";
    
    AppDomain myDomain = Thread.GetDomain();
    AssemblyName myAsmName = new AssemblyName();
    myAsmName.Name = "MyDynamicAssembly";    

    AssemblyBuilder myAsmBuilder = myDomain.DefineDynamicAssembly(
                        myAsmName,
                        AssemblyBuilderAccess.RunAndSave);

    myAsmBuilder.AddResourceFile("MyResource", myResourceFileName);

    // To confirm that the resource file has been added to the manifest,
    // we will save the assembly as MyAsm.dll. You can view the manifest
    // and confirm the presence of the resource file by running 
    // "ildasm MyAsm.dll" from the prompt in the directory where you executed
    // the compiled code. 

    myAsmBuilder.Save(myAsmFileName);    

    return myAsmBuilder;

   }

   public static void Main() 
   {

    FileStream myResourceFS = null;

    CreateResourceFile();

    Console.WriteLine("The contents of MyResource.txt, via GetFile:");

    AssemblyBuilder myAsm = BuildDynAssembly();

    try 
        {
       myResourceFS = myAsm.GetFile(myResourceFileName);
        }
    catch (NotSupportedException)
    {
       Console.WriteLine("---");
       Console.WriteLine("System.Reflection.Emit.AssemblyBuilder.GetFile\nis not supported " +
                 "in this SDK build.");
       Console.WriteLine("The file data will now be retrieved directly, via a new FileStream.");
       Console.WriteLine("---");
       myResourceFS = new FileStream(myResourceFileName, 
                     FileMode.Open);
    }
     
    StreamReader sr = new StreamReader(myResourceFS, System.Text.Encoding.ASCII);
    Console.WriteLine(sr.ReadToEnd());
    sr.Close();

   }

}


[C++] 

#using <mscorlib.dll>

using namespace System;
using namespace System::IO;
using namespace System::Threading;
using namespace System::Reflection;
using namespace System::Reflection::Emit;

__gc class AsmBuilderGetFileDemo {
public:
   static String* myResourceFileName = S"MyResource.txt";

   static FileInfo* CreateResourceFile() {
      FileInfo* f = new FileInfo(myResourceFileName);
      StreamWriter*  sw = f->CreateText();

      sw->WriteLine(S"Hello, world!");
      sw->Close();
      return f;
   }

   static AssemblyBuilder* BuildDynAssembly() {

      String* myAsmFileName = S"MyAsm.dll";

      AppDomain*  myDomain = Thread::GetDomain();
      AssemblyName* myAsmName = new AssemblyName();
      myAsmName->Name = S"MyDynamicAssembly";

      AssemblyBuilder*  myAsmBuilder = myDomain->DefineDynamicAssembly(myAsmName,
         AssemblyBuilderAccess::RunAndSave);

      myAsmBuilder->AddResourceFile(S"MyResource", myResourceFileName);

      // To confirm that the resource file has been added to the manifest,
      // we will save the assembly as MyAsm.dll. You can view the manifest
      // and confirm the presence of the resource file by running
      // "ildasm MyAsm.dll" from the prompt in the directory where you executed
      // the compiled code.

      myAsmBuilder->Save(myAsmFileName);

      return myAsmBuilder;
   }
};

int main() {

   FileStream* myResourceFS = 0;

   AsmBuilderGetFileDemo::CreateResourceFile();

   Console::WriteLine(S"The contents of MyResource.txt, via GetFile:");

   AssemblyBuilder*  myAsm = AsmBuilderGetFileDemo::BuildDynAssembly();

   try {
      myResourceFS = myAsm->GetFile(AsmBuilderGetFileDemo::myResourceFileName);
   } catch (NotSupportedException*) {
      Console::WriteLine(S"---");
      Console::WriteLine(S"System::Reflection::Emit::AssemblyBuilder::GetFile\nis not supported in this SDK build.");
      Console::WriteLine(S"The file data will now be retrieved directly, via a new FileStream.");
      Console::WriteLine(S"---");
      myResourceFS = new FileStream(AsmBuilderGetFileDemo::myResourceFileName,
         FileMode::Open);
   }

   StreamReader* sr = new StreamReader(myResourceFS, System::Text::Encoding::ASCII);
   Console::WriteLine(sr->ReadToEnd());
   sr->Close();
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

参照

AssemblyBuilder クラス | AssemblyBuilder メンバ | System.Reflection.Emit 名前空間