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

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

ModuleBuilder.DefineManifestResource メソッド

メモ : このメソッドは、.NET Framework version 2.0新しく追加されたものです。

動的アセンブリ埋め込まれるマニフェスト リソース BLOB定義します

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

Public Sub DefineManifestResource ( _
    name As String, _
    stream As Stream, _
    attribute As ResourceAttributes _
)
Dim instance As ModuleBuilder
Dim name As String
Dim stream As Stream
Dim attribute As ResourceAttributes

instance.DefineManifestResource(name, stream, attribute)
public void DefineManifestResource (
    string name,
    Stream stream,
    ResourceAttributes attribute
)
public:
void DefineManifestResource (
    String^ name, 
    Stream^ stream, 
    ResourceAttributes attribute
)
public void DefineManifestResource (
    String name, 
    Stream stream, 
    ResourceAttributes attribute
)
public function DefineManifestResource (
    name : String, 
    stream : Stream, 
    attribute : ResourceAttributes
)

パラメータ

name

リソースの、大文字と小文字区別される名前。

stream

リソースバイト格納しているストリーム

attribute

リソースパブリックかまたはプライベートかを指定する ResourceAttributes 値。

例外例外
例外種類条件

ArgumentNullException

namenull 参照 (Visual Basic では Nothing) です。

または

streamnull 参照 (Visual Basic では Nothing) です。

ArgumentException

name は、長さ 0 の文字列です。

InvalidOperationException

現在のモジュール格納している動的アセンブリ遷移的です。つまり、AssemblyBuilderAccess.Save フラグまたは AssemblyBuilderAccess.RunAndSave フラグ使用して作成されていません。

解説解説

アセンブリ マニフェスト記録されリソースは、マネージ リソースまたはマニフェスト リソース BLOB であり、どちらの場合もリンクまたは埋め込みによってアセンブリ含めることができます動的アセンブリでは、4 つシナリオすべてがサポートされています。

また、単一Win32 リソースを、AssemblyBuilder.DefineUnmanagedResource メソッドまたは ModuleBuilder.DefineUnmanagedResource メソッド使用してアセンブリ結びつけることもできます。このリソースは、アセンブリ マニフェスト表示されません。

使用例使用例

アンマネージ埋め込みリソース格納する EmittedManifestResourceAssembly.exe という名前の動的アセンブリ生成および保存するコード例次に示します。このコード例では、1 つモジュール構成されているアセンブリ作成し、アンマネージ リソース格納するメモリ ストリーム開きます。さらに、コードDefineManifestResource メソッド呼び出してリソース定義します

メモメモ

リソースに対してどのような種類ストリームでも使用できます。たとえば、ファイルからアンマネージ バイナリ データ読み込むこともできます

コード例では、Main メソッド使用して動的モジュールで型を定義しメソッド本体MSIL生成しますMain メソッド本体生成され、型が作成されると、コード例では、マニフェスト リソース関連付けられているストリームに 5 バイト書き込みますアセンブリ保存時にアセンブリリソース追加されます。

コード例実行後は、出力されアセンブリ実行できます出力されアセンブリMain メソッドコードは、埋め込まれマニフェスト リソース読み込みバイト値をコンソール出力しますMSIL 逆アセンブラ (Ildasm.exe) を使用すると、アセンブリ マニフェスト情報表示できます

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

Public Class Example
    
    Public Shared Sub Main()
 

        ' Define a dynamic assembly with one module. The module
        ' name and the assembly name are the same.
        Dim asmName As New
 AssemblyName("EmittedManifestResourceAssembly")
        Dim asmBuilder As AssemblyBuilder =
 _
            AppDomain.CurrentDomain.DefineDynamicAssembly( _
                asmName, _
                AssemblyBuilderAccess.RunAndSave _
            )
        Dim modBuilder As ModuleBuilder = _
            asmBuilder.DefineDynamicModule( _
                asmName.Name, _
                asmName.Name + ".exe" _
            )
        
        ' Create a memory stream for the unmanaged resource data.
        ' You can use any stream; for example, you might read the
        ' unmanaged resource data from a binary file. It is not
        ' necessary to put any data into the stream right now.
        Dim ms As New MemoryStream(1024)
        
        ' Define a public manifest resource with the name 
        ' "MyBinaryData, and associate it with the memory stream.
        modBuilder.DefineManifestResource( _
            "MyBinaryData", _
            ms, _
            ResourceAttributes.Public _
        )
        
        ' Create a type with a public static Main method that will
        ' be the entry point for the emitted assembly. 
        '
        ' The purpose of the Main method in this example is to read
 
        ' the manifest resource and display it, byte by byte.
        '
        Dim tb As TypeBuilder = modBuilder.DefineType("Example")
        Dim main As MethodBuilder = tb.DefineMethod(
 _
            "Main", _
            MethodAttributes.Public Or MethodAttributes.Static
 _
        )
        
        ' The Main method uses the Assembly type and the Stream
        ' type. 
        Dim asm As Type = GetType([Assembly])
        Dim str As Type = GetType(Stream)
        
        ' Get MethodInfo objects for the methods called by 
        ' Main.
        Dim getEx As MethodInfo = asm.GetMethod("GetExecutingAssembly")
        ' Use the overload of GetManifestResourceStream that 
        ' takes one argument, a string.
        Dim getMRS As MethodInfo = asm.GetMethod(
 _
            "GetManifestResourceStream", _
            New Type() {GetType(String)} _
        )
        Dim rByte As MethodInfo = str.GetMethod("ReadByte")
        ' Use the overload of WriteLine that writes an Int32.
        Dim write As MethodInfo = GetType(Console).GetMethod(
 _
            "WriteLine", _
            New Type() {GetType(Integer)} _
        )
        
        Dim ilg As ILGenerator = main.GetILGenerator()
        
        ' Main uses two local variables: the instance of the
        ' stream returned by GetManifestResourceStream, and 
        ' the value returned by ReadByte. The load and store 
        ' instructions refer to these locals by position  
        ' (0 and 1).
        Dim s As LocalBuilder = ilg.DeclareLocal(str)
        Dim b As LocalBuilder = ilg.DeclareLocal(GetType(Integer))
        
        ' Call the static Assembly.GetExecutingAssembly() method,
        ' which leaves the assembly instance on the stack. Push the
        ' string name of the resource on the stack, and call the
        ' GetManifestResourceStream(string) method of the assembly
        ' instance.
        ilg.EmitCall(OpCodes.Call, getEx, Nothing)
        ilg.Emit(OpCodes.Ldstr, "MyBinaryData")
        ilg.EmitCall(OpCodes.Callvirt, getMRS, Nothing)
        
        ' Store the Stream instance (actually an instance
        ' of UnmanagedMemoryStream, which derives from
        ' Stream).
        ilg.Emit(OpCodes.Stloc_0)
        
        ' Create a label, and associate it with this point
        ' in the emitted code.
        Dim theLoop As Label = ilg.DefineLabel()
        ilg.MarkLabel(theLoop)
        
        ' Load the Stream instance onto the stack, and call
        ' its ReadByte method. The return value is on the
        ' stack now; store it in location 1 (variable b).
        ilg.Emit(OpCodes.Ldloc_0)
        ilg.EmitCall(OpCodes.Callvirt, rByte, Nothing)
        ilg.Emit(OpCodes.Stloc_1)
        
        ' Load the value on the stack again, and call the
        ' WriteLine method to print it.
        ilg.Emit(OpCodes.Ldloc_1)
        ilg.EmitCall(OpCodes.Call, write, Nothing)
        
        ' Load the value one more time; load -1 (minus one)  
        ' and compare the two values. If return value from
        ' ReadByte was not -1, branch to the label 'loop'.
        ilg.Emit(OpCodes.Ldloc_1)
        ilg.Emit(OpCodes.Ldc_I4_M1)
        ilg.Emit(OpCodes.Ceq)
        ilg.Emit(OpCodes.Brfalse_S, theLoop)
        
        ' When all the bytes in the stream have been read,
        ' return. This is the end of Main.
        ilg.Emit(OpCodes.Ret)
        
        ' Create the type "Example" in the dynamic assembly.
        tb.CreateType()
        
        ' Because the manifest resource was added as an open
        ' stream, the data can be written at any time, right up
        ' until the assembly is saved. In this case, the data
        ' consists of five bytes.
        ms.Write(New Byte() {105, 36, 74, 97,
 109}, 0, 5)
        ms.SetLength(5)
        
        ' Set the Main method as the entry point for the 
        ' assembly, and save the assembly. The manifest resource
        ' is read from the memory stream, and appended to the
        ' end of the assembly. You can open the assembly with
        ' Ildasm and view the resource header for "MyBinaryData".
        asmBuilder.SetEntryPoint(main)
        asmBuilder.Save(asmName.Name + ".exe")
        
        Console.WriteLine("Now run EmittedManifestResourceAssembly.exe")
    
    End Sub 
End Class 

' This code example doesn't produce any output. The assembly it
' emits, EmittedManifestResourceAssembly.exe, produces the following
' output:
'
'105
'36
'74
'97
'109
'-1
'
using System;
using System.Reflection;
using System.Reflection.Emit;
using System.IO;

public class Example
{
    public static void Main()
    {
        // Define a dynamic assembly with one module. The module
        // name and the assembly name are the same.
        AssemblyName asmName = 
            new AssemblyName("EmittedManifestResourceAssembly");
        AssemblyBuilder asmBuilder =
            AppDomain.CurrentDomain.DefineDynamicAssembly(
                asmName,
                AssemblyBuilderAccess.RunAndSave
            ); 
        ModuleBuilder modBuilder = asmBuilder.DefineDynamicModule(
            asmName.Name,
            asmName.Name + ".exe"
        );

        // Create a memory stream for the unmanaged resource data.
        // You can use any stream; for example, you might read the
        // unmanaged resource data from a binary file. It is not
        // necessary to put any data into the stream right now.
        MemoryStream ms = new MemoryStream(1024);

        // Define a public manifest resource with the name 
        // "MyBinaryData, and associate it with the memory stream.
        modBuilder.DefineManifestResource(
            "MyBinaryData",
            ms,
            ResourceAttributes.Public
        );

        // Create a type with a public static Main method that will
        // be the entry point for the emitted assembly. 
        //
        // The purpose of the Main method in this example is to read
 
        // the manifest resource and display it, byte by byte.
        //
        TypeBuilder tb = modBuilder.DefineType("Example");
        MethodBuilder main = tb.DefineMethod("Main", 
            MethodAttributes.Public | MethodAttributes.Static
        );

        // The Main method uses the Assembly type and the Stream
        // type. 
        Type asm = typeof(Assembly);
        Type str = typeof(Stream);

        // Get MethodInfo objects for the methods called by 
        // Main.
        MethodInfo getEx = asm.GetMethod("GetExecutingAssembly");
        // Use the overload of GetManifestResourceStream that 
        // takes one argument, a string.
        MethodInfo getMRS = asm.GetMethod(
            "GetManifestResourceStream", 
            new Type[] {typeof(string)}
        );
        MethodInfo rByte = str.GetMethod("ReadByte");
        // Use the overload of WriteLine that writes an Int32.
        MethodInfo write = typeof(Console).GetMethod(
            "WriteLine", 
            new Type[] {typeof(int)}
        );

        ILGenerator ilg = main.GetILGenerator();

        // Main uses two local variables: the instance of the
        // stream returned by GetManifestResourceStream, and 
        // the value returned by ReadByte. The load and store 
        // instructions refer to these locals by position
        // (0 and 1).
        LocalBuilder s = ilg.DeclareLocal(str);
        LocalBuilder b = ilg.DeclareLocal(typeof(int));

        // Call the static Assembly.GetExecutingAssembly() method,
        // which leaves the assembly instance on the stack. Push the
        // string name of the resource on the stack, and call the
        // GetManifestResourceStream(string) method of the assembly
        // instance.
        ilg.EmitCall(OpCodes.Call, getEx, null);
        ilg.Emit(OpCodes.Ldstr, "MyBinaryData");
        ilg.EmitCall(OpCodes.Callvirt, getMRS, null);

        // Store the Stream instance (actually an instance
        // of UnmanagedMemoryStream, which derives from
        // Stream).
        ilg.Emit(OpCodes.Stloc_0);

        // Create a label, and associate it with this point
        // in the emitted code.
        Label loop = ilg.DefineLabel();
        ilg.MarkLabel(loop);

        // Load the Stream instance onto the stack, and call
        // its ReadByte method. The return value is on the
        // stack now; store it in location 1 (variable b).
        ilg.Emit(OpCodes.Ldloc_0);
        ilg.EmitCall(OpCodes.Callvirt, rByte, null);
        ilg.Emit(OpCodes.Stloc_1);

        // Load the value on the stack again, and call the
        // WriteLine method to print it.
        ilg.Emit(OpCodes.Ldloc_1);
        ilg.EmitCall(OpCodes.Call, write, null);

        // Load the value one more time; load -1 (minus one)  
        // and compare the two values. If return value from
        // ReadByte was not -1, branch to the label 'loop'.
        ilg.Emit(OpCodes.Ldloc_1);
        ilg.Emit(OpCodes.Ldc_I4_M1);
        ilg.Emit(OpCodes.Ceq);
        ilg.Emit(OpCodes.Brfalse_S, loop);

        // When all the bytes in the stream have been read,
        // return. This is the end of Main.
        ilg.Emit(OpCodes.Ret);

        // Create the type "Example" in the dynamic assembly.
        tb.CreateType();

        // Because the manifest resource was added as an open
        // stream, the data can be written at any time, right up
        // until the assembly is saved. In this case, the data
        // consists of five bytes.
        ms.Write(new byte[] { 105, 36, 74, 97, 109 }, 0, 5);
        ms.SetLength(5);

        // Set the Main method as the entry point for the 
        // assembly, and save the assembly. The manifest resource
        // is read from the memory stream, and appended to the
        // end of the assembly. You can open the assembly with
        // Ildasm and view the resource header for "MyBinaryData".
        asmBuilder.SetEntryPoint(main);
    asmBuilder.Save(asmName.Name + ".exe");

        Console.WriteLine("Now run EmittedManifestResourceAssembly.exe");
    }
}

/* This code example doesn't produce any output. The assembly it
   emits, EmittedManifestResourceAssembly.exe, produces the following
   output:

105
36
74
97
109
-1

 */
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
ModuleBuilder クラス
ModuleBuilder メンバ
System.Reflection.Emit 名前空間
ModuleBuilder.DefineResource
AssemblyBuilder.DefineResource
ResourceWriter.AddResource
AssemblyBuilder.AddResourceFile
AssemblyBuilder.DefineUnmanagedResource
ModuleBuilder.DefineUnmanagedResource



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

辞書ショートカット

すべての辞書の索引

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

   

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



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

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

©2024 GRAS Group, Inc.RSS