AssemblyBuilder.AddResourceFile メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > AssemblyBuilder.AddResourceFile メソッドの意味・解説 

AssemblyBuilder.AddResourceFile メソッド (String, String, ResourceAttributes)

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

名前空間: System.Reflection.Emit
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Sub AddResourceFile ( _
    name As String, _
    fileName As String, _
    attribute As ResourceAttributes _
)
Dim instance As AssemblyBuilder
Dim name As String
Dim fileName As String
Dim attribute As ResourceAttributes

instance.AddResourceFile(name, fileName, attribute)
public void AddResourceFile (
    string name,
    string fileName,
    ResourceAttributes attribute
)
public:
void AddResourceFile (
    String^ name, 
    String^ fileName, 
    ResourceAttributes attribute
)
public void AddResourceFile (
    String name, 
    String fileName, 
    ResourceAttributes attribute
)
public function AddResourceFile (
    name : String, 
    fileName : String, 
    attribute : ResourceAttributes
)

パラメータ

name

リソース論理名。

fileName

論理名を割り当てる対象物理ファイル名 (.resources ファイル)。これには、パス含めないようにします。このファイルは、追加先のアセンブリと同じディレクトリ配置する必要があります

attribute

リソース属性

例外例外
例外種類条件

ArgumentException

name が既に定義されています。

または

アセンブリ内に fileName という名前のファイルがもう 1 つあります

または

name長さが 0 か、fileName長さが 0 です。

または

fileNameパス含まれています。

ArgumentNullException

name または fileNamenull 参照 (Visual Basic では Nothing) です。

FileNotFoundException

fileName指定したファイルが見つからない場合

SecurityException

呼び出し元に必要なアクセス許可がありません。

解説解説
使用例使用例

次のコード例は、AddResourceFile使用して動的に作成されアセンブリリソース ファイル結び付ける方法示してます。

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

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

   }

}

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

import System.*;
import System.IO.*;
import System.Threading.*;
import System.Reflection.*;
import 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;
    } //CreateResourceFile
    
    private static AssemblyBuilder BuildDynAssembly()
    {
        String myAsmFileName = "MyAsm.dll";
        AppDomain myDomain = System.Threading.Thread.GetDomain();
        AssemblyName myAsmName = new AssemblyName();
        myAsmName.set_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;
    } //BuildDynAssembly
    
    public static void main(String[]
 args)
    {
        FileStream myResourceFS = null;
        CreateResourceFile();
        Console.WriteLine("The contents of MyResource.txt, via GetFile:");
        AssemblyBuilder myAsm = BuildDynAssembly();
        try {
            myResourceFS = myAsm.GetFile(myResourceFileName);
        }
        catch (NotSupportedException exp) {
            Console.WriteLine("---");
            Console.WriteLine(
                ("System.Reflection.Emit.AssemblyBuilder.GetFile \n " 
                + "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);
        }
        StreamReader sr = new StreamReader(myResourceFS,
            System.Text.Encoding.get_ASCII());
        Console.WriteLine(sr.ReadToEnd());
        sr.Close();
    } //main 
} //AsmBuilderGetFileDemo
.NET Framework のセキュリティ.NET Frameworkセキュリティ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
AssemblyBuilder クラス
AssemblyBuilder メンバ
System.Reflection.Emit 名前空間

AssemblyBuilder.AddResourceFile メソッド (String, String)

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

名前空間: System.Reflection.Emit
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

例外例外
例外種類条件

ArgumentException

name が既に定義されています。

または

アセンブリ内に fileName という名前のファイルがもう 1 つあります

または

name長さが 0 です。

または

fileName長さゼロか、fileNameパス含まれています。

ArgumentNullException

name または fileNamenull 参照 (Visual Basic では Nothing) です。

FileNotFoundException

ファイル fileName が見つかりません。

SecurityException

呼び出し元に必要なアクセス許可がありません。

解説解説
使用例使用例

次のコード例は、AddResourceFile使用して動的に作成されアセンブリリソース ファイル結び付ける方法示してます。

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

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

   }

}

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

import System.*;
import System.IO.*;
import System.Threading.*;
import System.Reflection.*;
import 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;
    } //CreateResourceFile
    
    private static AssemblyBuilder BuildDynAssembly()
    {
        String myAsmFileName = "MyAsm.dll";
        AppDomain myDomain = System.Threading.Thread.GetDomain();
        AssemblyName myAsmName = new AssemblyName();
        myAsmName.set_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;
    } //BuildDynAssembly
    
    public static void main(String[]
 args)
    {
        FileStream myResourceFS = null;
        CreateResourceFile();
        Console.WriteLine("The contents of MyResource.txt, via GetFile:");
        AssemblyBuilder myAsm = BuildDynAssembly();
        try {
            myResourceFS = myAsm.GetFile(myResourceFileName);
        }
        catch (NotSupportedException exp) {
            Console.WriteLine("---");
            Console.WriteLine(
                ("System.Reflection.Emit.AssemblyBuilder.GetFile \n " 
                + "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);
        }
        StreamReader sr = new StreamReader(myResourceFS,
            System.Text.Encoding.get_ASCII());
        Console.WriteLine(sr.ReadToEnd());
        sr.Close();
    } //main 
} //AsmBuilderGetFileDemo
.NET Framework のセキュリティ.NET Frameworkセキュリティ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
AssemblyBuilder クラス
AssemblyBuilder メンバ
System.Reflection.Emit 名前空間

AssemblyBuilder.AddResourceFile メソッド

既存リソース ファイルをこのアセンブリ追加します
オーバーロードの一覧オーバーロードの一覧

名前 説明
AssemblyBuilder.AddResourceFile (String, String) 既存リソース ファイルをこのアセンブリ追加します
AssemblyBuilder.AddResourceFile (String, String, ResourceAttributes) 既存リソース ファイルをこのアセンブリ追加します
参照参照

関連項目

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



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「AssemblyBuilder.AddResourceFile メソッド」の関連用語

AssemblyBuilder.AddResourceFile メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



AssemblyBuilder.AddResourceFile メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS