AssemblyBuilder.AddResourceFile 메서드

정의

이 어셈블리에 기존 리소스 파일을 추가합니다.

오버로드

AddResourceFile(String, String)

이 어셈블리에 기존 리소스 파일을 추가합니다.

AddResourceFile(String, String, ResourceAttributes)

이 어셈블리에 기존 리소스 파일을 추가합니다.

AddResourceFile(String, String)

이 어셈블리에 기존 리소스 파일을 추가합니다.

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

리소스의 논리적 이름입니다.

fileName
String

논리적 이름이 매핑되는 물리적 파일 이름(.resources 파일)입니다. 경로가 포함되지 않아야 합니다. 해당 파일은 이 파일이 추가되는 어셈블리와 동일한 디렉터리에 있어야 합니다.

예외

name이 이전에 정의되었습니다.

또는

어셈블리에 이름이 fileName인 다른 파일이 있습니다.

또는

name의 길이가 0입니다.

또는

fileName의 길이가 0이거나 fileName에 경로가 포함되어 있는 경우

name 또는 fileNamenull인 경우

fileName 파일을 찾을 수 없는 경우

호출자에게 필요한 권한이 없는 경우

예제

다음 코드 샘플에서는 를 사용하여 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 는 다른 지속 가능한 모듈, 독립 실행형 관리형 리소스 또는 독립 실행형 매니페스트 파일과 동일하지 않아야 합니다.

파일의 관리되는 리소스는 공용으로 간주됩니다.

지정된 리소스 파일은 어셈블리가 저장될 디렉터리에 있어야 합니다.

참고

.NET Framework 2.0 서비스 팩 1부터 이 멤버는 ReflectionPermission 더 이상 플래그가 ReflectionPermissionFlag.ReflectionEmit 필요하지 않습니다. (리플렉션 내보내기의 보안 문제를 참조하세요.) 이 기능을 사용하려면 애플리케이션이 .NET Framework 3.5 이상을 대상으로 해야 합니다.

적용 대상

AddResourceFile(String, String, ResourceAttributes)

이 어셈블리에 기존 리소스 파일을 추가합니다.

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

리소스의 논리적 이름입니다.

fileName
String

논리적 이름이 매핑되는 물리적 파일 이름(.resources 파일)입니다. 경로가 포함되지 않아야 합니다. 해당 파일은 이 파일이 추가되는 어셈블리와 동일한 디렉터리에 있어야 합니다.

attribute
ResourceAttributes

리소스 특성입니다.

예외

name이 이전에 정의되었습니다.

또는

어셈블리에 이름이 fileName인 다른 파일이 있습니다.

또는

name의 길이가 0이거나 fileName의 길이가 0인 경우

또는

fileName에 경로가 포함되어 있습니다.

name 또는 fileNamenull인 경우

fileName 파일을 찾을 수 없는 경우

호출자에게 필요한 권한이 없는 경우

예제

다음 코드 샘플에서는 를 사용하여 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 는 다른 지속 가능한 모듈, 독립 실행형 관리형 리소스 또는 독립 실행형 매니페스트 파일과 동일하지 않아야 합니다.

관리되는 리소스에 대해 특성을 지정할 수 있습니다.

지정된 리소스 파일은 어셈블리가 저장될 디렉터리에 있어야 합니다.

참고

.NET Framework 2.0 서비스 팩 1부터 이 멤버는 ReflectionPermission 더 이상 플래그가 ReflectionPermissionFlag.ReflectionEmit 필요하지 않습니다. (리플렉션 내보내기의 보안 문제를 참조하세요.) 이 기능을 사용하려면 애플리케이션이 .NET Framework 3.5 이상을 대상으로 해야 합니다.

적용 대상