AssemblyBuilder.AddResourceFile メソッド

定義

このアセンブリに既存のリソース ファイルを追加します。Adds an existing resource file to this assembly.

オーバーロード

AddResourceFile(String, String)

このアセンブリに既存のリソース ファイルを追加します。Adds an existing resource file to this assembly.

AddResourceFile(String, String, ResourceAttributes)

このアセンブリに既存のリソース ファイルを追加します。Adds an existing resource file to this assembly.

AddResourceFile(String, String)

このアセンブリに既存のリソース ファイルを追加します。Adds an existing resource file to this assembly.

public:
 void AddResourceFile(System::String ^ name, System::String ^ fileName);
public void AddResourceFile (string name, string fileName);
member this.AddResourceFile : string * string -> unit
Public Sub AddResourceFile (name As String, fileName As String)

パラメーター

name
String

リソースの論理名。The logical name of the resource.

fileName
String

論理名のマップ先物理ファイル名 (.resources ファイル)。The physical file name (.resources file) to which the logical name is mapped. これにパスが含まれていてはなりません。ファイルは、追加先アセンブリと同じディレクトリの中になければなりません。This should not include a path; the file must be in the same directory as the assembly to which it is added.

例外

name は以前に定義されています。name has been previously defined.

または-or- アセンブリには、fileName という名前の別のファイルがあります。There is another file in the assembly named fileName.

または-or- name の長さが 0 です。The length of name is zero.

または-or- fileName の長さが 0 か、または fileName にパスが含まれる場合。The length of fileName is zero, or if fileName includes a path.

name または fileNamenull です。name or fileName is null.

ファイル fileName が見つかりません。The file fileName is not found.

呼び出し元に、必要なアクセス許可がありません。The caller does not have the required permission.

次のコードサンプルは、AddResourceFileを使用して、動的に作成されたアセンブリにリソースファイルをアタッチする方法を示しています。The following code sample demonstrates how to attach a resource file to a dynamically created assembly, using AddResourceFile.

using namespace System;
using namespace System::IO;
using namespace System::Threading;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
ref class AsmBuilderGetFileDemo
{
public:
   static String^ myResourceFileName = "MyResource.txt";
   static FileInfo^ CreateResourceFile()
   {
      FileInfo^ f = gcnew FileInfo( myResourceFileName );
      StreamWriter^ sw = f->CreateText();
      sw->WriteLine( "Hello, world!" );
      sw->Close();
      return f;
   }

   static AssemblyBuilder^ BuildDynAssembly()
   {
      String^ myAsmFileName = "MyAsm.dll";
      AppDomain^ myDomain = Thread::GetDomain();
      AssemblyName^ myAsmName = gcnew 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;
   }

};

int main()
{
   FileStream^ myResourceFS = nullptr;
   AsmBuilderGetFileDemo::CreateResourceFile();
   Console::WriteLine( "The contents of MyResource.txt, via GetFile:" );
   AssemblyBuilder^ myAsm = AsmBuilderGetFileDemo::BuildDynAssembly();
   try
   {
      myResourceFS = myAsm->GetFile( AsmBuilderGetFileDemo::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 = gcnew FileStream( AsmBuilderGetFileDemo::myResourceFileName,FileMode::Open );
   }

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


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


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

End Class

注釈

fileName は、他の永続モジュール、スタンドアロンマネージリソース、またはスタンドアロンマニフェストファイルと同じにすることはできません。fileName should not be the same as that of any other persistable module, standalone managed resource, or the standalone manifest file.

ファイル内のマネージリソースは、パブリックであると見なされます。The managed resources in the file are assumed to be public.

指定されたリソースファイルは、アセンブリが保存されるディレクトリに存在する必要があります。The specified resource file must be in the directory where the assembly will be saved.

注意

.NET Framework 2.0 Service Pack 1.NET Framework 2.0 Service Pack 1以降では、このメンバーは ReflectionPermissionFlag.ReflectionEmit フラグで ReflectionPermission する必要がなくなりました。Starting with the .NET Framework 2.0 Service Pack 1.NET Framework 2.0 Service Pack 1, this member no longer requires ReflectionPermission with the ReflectionPermissionFlag.ReflectionEmit flag. (「リフレクション出力のセキュリティの問題」を参照してください)。この機能を使用するには、アプリケーションで .NET Framework 3.5.NET Framework 3.5 以降を対象にする必要があります。(See Security Issues in Reflection Emit.) To use this functionality, your application should target the .NET Framework 3.5.NET Framework 3.5 or later.

AddResourceFile(String, String, ResourceAttributes)

このアセンブリに既存のリソース ファイルを追加します。Adds an existing resource file to this assembly.

public:
 void AddResourceFile(System::String ^ name, System::String ^ fileName, System::Reflection::ResourceAttributes attribute);
public void AddResourceFile (string name, string fileName, System.Reflection.ResourceAttributes attribute);
member this.AddResourceFile : string * string * System.Reflection.ResourceAttributes -> unit
Public Sub AddResourceFile (name As String, fileName As String, attribute As ResourceAttributes)

パラメーター

name
String

リソースの論理名。The logical name of the resource.

fileName
String

論理名のマップ先物理ファイル名 (.resources ファイル)。The physical file name (.resources file) to which the logical name is mapped. これにパスが含まれていてはなりません。ファイルは、追加先アセンブリと同じディレクトリの中になければなりません。This should not include a path; the file must be in the same directory as the assembly to which it is added.

attribute
ResourceAttributes

リソースの属性。The resource attributes.

例外

name は以前に定義されています。name has been previously defined.

または-or- アセンブリには、fileName という名前の別のファイルがあります。There is another file in the assembly named fileName.

または-or- name の長さが 0、または fileName の長さが 0 の場合。The length of name is zero or if the length of fileName is zero.

または-or- fileName にパスが含まれています。fileName includes a path.

name または fileNamenull です。name or fileName is null.

ファイル fileName が見つからない場合。If the file fileName is not found.

呼び出し元に、必要なアクセス許可がありません。The caller does not have the required permission.

次のコードサンプルは、AddResourceFileを使用して、動的に作成されたアセンブリにリソースファイルをアタッチする方法を示しています。The following code sample demonstrates how to attach a resource file to a dynamically created assembly, using AddResourceFile.

using namespace System;
using namespace System::IO;
using namespace System::Threading;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
ref class AsmBuilderGetFileDemo
{
public:
   static String^ myResourceFileName = "MyResource.txt";
   static FileInfo^ CreateResourceFile()
   {
      FileInfo^ f = gcnew FileInfo( myResourceFileName );
      StreamWriter^ sw = f->CreateText();
      sw->WriteLine( "Hello, world!" );
      sw->Close();
      return f;
   }

   static AssemblyBuilder^ BuildDynAssembly()
   {
      String^ myAsmFileName = "MyAsm.dll";
      AppDomain^ myDomain = Thread::GetDomain();
      AssemblyName^ myAsmName = gcnew 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;
   }

};

int main()
{
   FileStream^ myResourceFS = nullptr;
   AsmBuilderGetFileDemo::CreateResourceFile();
   Console::WriteLine( "The contents of MyResource.txt, via GetFile:" );
   AssemblyBuilder^ myAsm = AsmBuilderGetFileDemo::BuildDynAssembly();
   try
   {
      myResourceFS = myAsm->GetFile( AsmBuilderGetFileDemo::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 = gcnew FileStream( AsmBuilderGetFileDemo::myResourceFileName,FileMode::Open );
   }

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


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


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

End Class

注釈

fileName は、他の永続モジュール、スタンドアロンマネージリソース、またはスタンドアロンマニフェストファイルと同じにすることはできません。fileName should not be the same as that of any other persistable module, standalone managed resource, or the standalone manifest file.

属性には、マネージド リソースを指定できます。Attributes can be specified for the managed resource.

指定されたリソースファイルは、アセンブリが保存されるディレクトリに存在する必要があります。The specified resource file must be in the directory where the assembly will be saved.

注意

.NET Framework 2.0 Service Pack 1.NET Framework 2.0 Service Pack 1以降では、このメンバーは ReflectionPermissionFlag.ReflectionEmit フラグで ReflectionPermission する必要がなくなりました。Starting with the .NET Framework 2.0 Service Pack 1.NET Framework 2.0 Service Pack 1, this member no longer requires ReflectionPermission with the ReflectionPermissionFlag.ReflectionEmit flag. (「リフレクション出力のセキュリティの問題」を参照してください)。この機能を使用するには、アプリケーションで .NET Framework 3.5.NET Framework 3.5 以降を対象にする必要があります。(See Security Issues in Reflection Emit.) To use this functionality, your application should target the .NET Framework 3.5.NET Framework 3.5 or later.

適用対象