ModuleBuilder.DefineManifestResource 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
定義一個二進位大型物件(BLOB),代表一個要嵌入動態組裝中的清單資源。
public:
void DefineManifestResource(System::String ^ name, System::IO::Stream ^ stream, System::Reflection::ResourceAttributes attribute);
public void DefineManifestResource(string name, System.IO.Stream stream, System.Reflection.ResourceAttributes attribute);
member this.DefineManifestResource : string * System.IO.Stream * System.Reflection.ResourceAttributes -> unit
Public Sub DefineManifestResource (name As String, stream As Stream, attribute As ResourceAttributes)
參數
- name
- String
資源的區分大小寫名稱。
- stream
- Stream
一個包含該資源位元組的串流。
- attribute
- ResourceAttributes
一個列舉值,用來指定資源是公共還是私有。
例外狀況
name 是一條零長度的字串。
包含當前模組的動態組件是瞬態的;也就是說,呼叫時 DefineDynamicModule(String, String) 未指定檔名。
範例
以下範例產生並儲存一個名為 EmittedManifestResourceAssembly.exe的動態組合,其中包含一個嵌入的非管理資源。 範例中建立組合語言,該組合語言由一個模組組成,並開啟記憶體串流以包含未受管理的資源。 接著程式碼呼叫該 DefineManifestResource 方法來定義資源。
Note
你可以用任何類型的溪流作為資源;例如,你可以從檔案讀取未管理的二進位資料。
範例中定義動態模組中的一種型別,並 Main 產生方法主體的 MSIL。 在產生方法主體 Main 並建立型別後,程式碼範例會寫入五個位元組到與 manifest 資源相關的串流。 當組裝檔被儲存時,資源會被附加到它上。
執行範例後,你可以執行發射的組件。 發射組件方法 Main 中的程式碼會讀取嵌入的清單資源,並將位元組值列印到主控台。 你可以使用 Ildasm.exe(IL 反組譯器) 來查看組合清單中的資訊。
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.
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
*/
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.
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
'
備註
在組裝清單中記錄的資源可以是受管理資源或顯現資源 BLOB,這些資源都可以透過連結或嵌入方式納入組裝。 動態組裝皆支援這四種情境。
此方法允許您將 manifest 資源 BLOB 嵌入動態組裝中。
若要將受管理資源嵌入動態組件的清單模組或衛星模組,請使用 ModuleBuilder.DefineResource 取得資源編譯器的方法,並使用 ResourceWriter.AddResource 該方法新增資源。
要將受管理資源連結到動態組裝,請使用 AssemblyBuilder.DefineResource 該方法取得資源撰寫器,並用該 ResourceWriter.AddResource 方法新增連結資源。
要將清單資源 BLOB 連結到動態組裝,請使用 AssemblyBuilder.AddResourceFile 新增該連結資源的方法。
此外,也可以透過使用 方法 AssemblyBuilder.DefineUnmanagedResource 或 方法 ModuleBuilder.DefineUnmanagedResource ,將單一 Win32 資源附加到組件中。 此資源不會出現在組裝清單中。