共用方式為


AssemblyBuilder.DefineUnmanagedResource 方法

定義

定義這個組件的 Unmanaged 資源。

多載

DefineUnmanagedResource(String)

定義指定資源檔名稱的這個組件之 Unmanaged 資源檔。

DefineUnmanagedResource(Byte[])

將這個組件的 Unmanaged 資源定義為不透明的 Blob (位元組)。

DefineUnmanagedResource(String)

定義指定資源檔名稱的這個組件之 Unmanaged 資源檔。

public:
 void DefineUnmanagedResource(System::String ^ resourceFileName);
public void DefineUnmanagedResource (string resourceFileName);
member this.DefineUnmanagedResource : string -> unit
Public Sub DefineUnmanagedResource (resourceFileName As String)

參數

resourceFileName
String

資源檔名稱。

例外狀況

先前定義的 Unmanaged 資源。

-或-

檔案 resourceFileName 不可讀取。

-或-

resourceFileName 為空字串 ("")。

resourceFileNamenull

找不到 resourceFileName

-或-

resourceFileName 是目錄。

呼叫端沒有必要的權限。

範例

下列範例示範對的 DefineUnmanagedResource呼叫,並傳遞外部資源檔。

void main()
{
   AssemblyBuilder^ myAssembly = CreateAssembly("MyEmitTestAssembly");
   
   // Defines a standalone managed resource for this assembly.
   IResourceWriter^ myResourceWriter = 
      myAssembly->DefineResource( "myResourceFile", "A sample Resource File", 
         "MyAssemblyResource.resources", ResourceAttributes::Private );

   myResourceWriter->AddResource( "AddResource Test", "Testing for the added resource" );

   myAssembly->Save(myAssembly->GetName()->Name + ".dll" );
   
   // Defines an unmanaged resource file for this assembly.
   myAssembly->DefineUnmanagedResource(  "MyAssemblyResource.resources" );
};
   internal static void Main()
   {
      AssemblyBuilder myAssembly = CreateAssembly("MyEmitTestAssembly");

      // Defines a standalone managed resource for this assembly.
      IResourceWriter myResourceWriter = myAssembly.DefineResource("myResourceFile",
         "A sample Resource File", "MyAssemblyResource.resources",
         ResourceAttributes.Private);

      myResourceWriter.AddResource("AddResource Test", "Testing for the added resource");

      myAssembly.Save(myAssembly.GetName().Name + ".dll");

      // Defines an unmanaged resource file for this assembly.
      myAssembly.DefineUnmanagedResource("MyAssemblyResource.resources");
   }

   private static AssemblyBuilder CreateAssembly(string name)
   {
      AssemblyName aName = new AssemblyName(name);
      AssemblyBuilder myAssembly =
         AppDomain.CurrentDomain.DefineDynamicAssembly(aName,
            AssemblyBuilderAccess.Save);

      // Define a dynamic module.
      ModuleBuilder myModule =
         myAssembly.DefineDynamicModule(aName.Name, aName.Name + ".dll");

      // Define a public class named "EmitClass" in the assembly.
      TypeBuilder myEmitClass = myModule.DefineType("EmitClass", TypeAttributes.Public);

      // Define the Display method.
      MethodBuilder myMethod = myEmitClass.DefineMethod("Display",
         MethodAttributes.Public, typeof(String), null);

      // Generate IL for Display method.
      ILGenerator methodIL = myMethod.GetILGenerator();
      methodIL.Emit(OpCodes.Ldstr, "Display method gets called.");
      methodIL.Emit(OpCodes.Ret);

      myEmitClass.CreateType();

      return(myAssembly);
   }
}
Friend Shared Sub Main()
   Dim myAssembly As AssemblyBuilder = _
       CreateAssembly("MyEmitTestAssembly")
   
   ' Defines a standalone managed resource for this assembly.
   Dim myResourceWriter As IResourceWriter = _
      myAssembly.DefineResource("myResourceFile", _
         "A sample Resource File", "MyAssemblyResource.resources", _
         ResourceAttributes.Private)

   myResourceWriter.AddResource("AddResource Test", "Testing for the added resource")

   myAssembly.Save(myAssembly.GetName().Name & ".dll")
   
   ' Defines an unmanaged resource file for this assembly.
   myAssembly.DefineUnmanagedResource("MyAssemblyResource.resources")

End Sub 

Private Shared Function CreateAssembly(ByVal name As String) As AssemblyBuilder

   Dim aName As New AssemblyName(name)

   Dim myAssembly As AssemblyBuilder = _
      AppDomain.CurrentDomain.DefineDynamicAssembly(aName, _
         AssemblyBuilderAccess.Save)
   
   ' Define a dynamic module.
   Dim myModule As ModuleBuilder = _
      myAssembly.DefineDynamicModule(aName.Name, aName.Name & ".dll")

   ' Define a public class named "EmitClass" in the assembly.
   Dim myEmitClass As TypeBuilder = _
      myModule.DefineType("EmitClass", TypeAttributes.Public)
   
   ' Define the Display method.
   Dim myMethod As MethodBuilder = _
      myEmitClass.DefineMethod("Display", MethodAttributes.Public, _
                                             GetType(String), Nothing)
   
   ' Generate IL for Display method.
   Dim methodIL As ILGenerator = myMethod.GetILGenerator()
   methodIL.Emit(OpCodes.Ldstr, "Display method get called.")
   methodIL.Emit(OpCodes.Ret)
   
   myEmitClass.CreateType()

   Return myAssembly

End Function

備註

元件只能與一個非受控資源相關聯。 這表示 DefineVersionInfoResource 在先前呼叫其中一個方法之後呼叫 或 DefineUnmanagedResource 之後,將會擲回 System.ArgumentException。 多個非受控資源必須與 Microsoft ResMerge 公用程式之類的工具合併, (Common Language Runtime) 未提供。

注意

從 .NET Framework 2.0 Service Pack 1 開始,此成員不再需要 ReflectionPermissionReflectionPermissionFlag.ReflectionEmit標。 (請參閱反映發出中的安全性問題.) 若要使用這項功能,您的應用程式應以 .NET Framework 3.5 或更新版本為目標。

適用於

DefineUnmanagedResource(Byte[])

將這個組件的 Unmanaged 資源定義為不透明的 Blob (位元組)。

public:
 void DefineUnmanagedResource(cli::array <System::Byte> ^ resource);
public void DefineUnmanagedResource (byte[] resource);
member this.DefineUnmanagedResource : byte[] -> unit
Public Sub DefineUnmanagedResource (resource As Byte())

參數

resource
Byte[]

表示 Unmanaged 資源的不透明 Blob (位元組)。

例外狀況

先前定義的 Unmanaged 資源。

resourcenull

呼叫端沒有必要的權限。

範例

下列程式代碼範例會使用 DefineUnmanagedResource,建立並附加位元組數位列,代表非受控資源至動態元件。

/*
   The following program demonstrates the 'DefineResource' and 'DefineUnmanagedResource'
   methods of 'AssemblyBuilder' class. It builds an assembly and a resource file at runtime.
   An unmanaged resource file is also defined for the same resource file. The EmittedTest2.cpp file
   calls the methods of "MyEmitAssembly.dll" assembly and the message is displayed to console.
*/
using namespace System;
using namespace System::Threading;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
using namespace System::Resources;

   static AssemblyBuilder^ CreateAssembly( String^ name )
   {
      AssemblyName^ aName = gcnew AssemblyName(name);
      AssemblyBuilder^ myAssembly = 
         AppDomain::CurrentDomain->DefineDynamicAssembly( aName, 
            AssemblyBuilderAccess::Save );
      
      // Define a dynamic module.
      ModuleBuilder^ myModule = 
         myAssembly->DefineDynamicModule( aName->Name, aName->Name + ".dll" );
      
      // Define a public class named "EmitClass" in the assembly.
      TypeBuilder^ myEmitClass = 
         myModule->DefineType( "EmitClass", TypeAttributes::Public );
      
      // Define the Display method.
      MethodBuilder^ myMethod = 
         myEmitClass->DefineMethod( "Display", MethodAttributes::Public, 
            String::typeid, nullptr );
      
      // Generate IL for Display method.
      ILGenerator^ methodIL = myMethod->GetILGenerator();
      methodIL->Emit( OpCodes::Ldstr, "Display method gets called." );
      methodIL->Emit( OpCodes::Ret );
      
      myEmitClass->CreateType();

      return (myAssembly);
   };

   void main()
   {
      AssemblyBuilder^ myAssembly = CreateAssembly("MyEmitTestAssembly");
      
      // Defines a standalone managed resource for this assembly.
      IResourceWriter^ myResourceWriter = 
         myAssembly->DefineResource( "myResourceFile", "A sample Resource File", 
            "MyAssemblyResource.resources", ResourceAttributes::Private );

      myResourceWriter->AddResource( "AddResource Test", "Testing for the added resource" );

      myAssembly->Save(myAssembly->GetName()->Name + ".dll" );
      
      // Defines an unmanaged resource file for this assembly.
      myAssembly->DefineUnmanagedResource( gcnew array<Byte>{01, 00, 01} );
   };
using System;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;
using System.Resources;

public class MyAssemblyResource
{
   internal static void Main()
   {
      AssemblyBuilder myAssembly = CreateAssembly("MyEmitTestAssembly");

      // Defines a standalone managed resource for this assembly.
      IResourceWriter myResourceWriter = myAssembly.DefineResource("myResourceFile",
         "A sample Resource File", "MyAssemblyResource.resources",
         ResourceAttributes.Private);

      myResourceWriter.AddResource("AddResource Test", "Testing for the added resource");

      myAssembly.Save(myAssembly.GetName().Name + ".dll");

      // Defines an unmanaged resource file for this assembly.
      myAssembly.DefineUnmanagedResource(new byte[]{01, 00, 01});
   }

   private static AssemblyBuilder CreateAssembly(string name)
   {
      AssemblyName aName = new AssemblyName(name);
      AssemblyBuilder myAssembly =
         AppDomain.CurrentDomain.DefineDynamicAssembly(aName,
            AssemblyBuilderAccess.Save);

      // Define a dynamic module.
      ModuleBuilder myModule =
         myAssembly.DefineDynamicModule(aName.Name, aName.Name + ".dll");

      // Define a public class named "EmitClass" in the assembly.
      TypeBuilder myEmitClass = myModule.DefineType("EmitClass", TypeAttributes.Public);

      // Define the Display method.
      MethodBuilder myMethod = myEmitClass.DefineMethod("Display",
         MethodAttributes.Public, typeof(String), null);

      // Generate IL for Display method.
      ILGenerator methodIL = myMethod.GetILGenerator();
      methodIL.Emit(OpCodes.Ldstr, "Display method gets called.");
      methodIL.Emit(OpCodes.Ret);

      myEmitClass.CreateType();

      return(myAssembly);
   }
}
Imports System.Threading
Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Resources

Public Class MyAssemblyResource
   
   Friend Shared Sub Main()
      Dim myAssembly As AssemblyBuilder = _
          CreateAssembly("MyEmitTestAssembly")
      
      ' Defines a standalone managed resource for this assembly.
      Dim myResourceWriter As IResourceWriter = _
         myAssembly.DefineResource("myResourceFile", _
            "A sample Resource File", "MyAssemblyResource.resources", _
            ResourceAttributes.Private)

      myResourceWriter.AddResource("AddResource Test", "Testing for the added resource")

      myAssembly.Save(myAssembly.GetName().Name & ".dll")
      
      ' Defines an unmanaged resource file for this assembly.
      myAssembly.DefineUnmanagedResource(New Byte() {1, 0, 1})

   End Sub 
   
   Private Shared Function CreateAssembly(ByVal name As String) As AssemblyBuilder

      Dim aName As New AssemblyName(name)

      Dim myAssembly As AssemblyBuilder = _
         AppDomain.CurrentDomain.DefineDynamicAssembly(aName, _
            AssemblyBuilderAccess.Save)
      
      ' Define a dynamic module.
      Dim myModule As ModuleBuilder = _
         myAssembly.DefineDynamicModule(aName.Name, aName.Name & ".dll")

      ' Define a public class named "EmitClass" in the assembly.
      Dim myEmitClass As TypeBuilder = _
         myModule.DefineType("EmitClass", TypeAttributes.Public)
      
      ' Define the Display method.
      Dim myMethod As MethodBuilder = _
         myEmitClass.DefineMethod("Display", MethodAttributes.Public, _
                                                GetType(String), Nothing)
      
      ' Generate IL for Display method.
      Dim methodIL As ILGenerator = myMethod.GetILGenerator()
      methodIL.Emit(OpCodes.Ldstr, "Display method get called.")
      methodIL.Emit(OpCodes.Ret)
      
      myEmitClass.CreateType()

      Return myAssembly

   End Function 
End Class

備註

元件只能與一個非受控資源相關聯。 這表示 DefineVersionInfoResource 在先前呼叫其中一個方法之後呼叫 或 DefineUnmanagedResource 之後,將會擲回要擲回的 System.ArgumentException。 多個非受控資源必須與 Microsoft ResMerge 公用程式之類的工具合併, (Common Language Runtime) 未提供。

適用於