AssemblyBuilder.DefineUnmanagedResource Método

Definición

Define un recurso no administrado para este ensamblado.

Sobrecargas

DefineUnmanagedResource(String)

Define un archivo de recurso no administrado para este ensamblado con el nombre del archivo de recursos.

DefineUnmanagedResource(Byte[])

Define un recurso no administrado para este ensamblado como blob opaco de bytes.

DefineUnmanagedResource(String)

Define un archivo de recurso no administrado para este ensamblado con el nombre del archivo de recursos.

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

Parámetros

resourceFileName
String

Nombre del archivo de recursos.

Excepciones

Se definió anteriormente un recurso no administrado.

o bien

El archivo resourceFileName no es legible.

o bien

resourceFileName es la cadena vacía ("").

resourceFileName es null.

No se ha encontrado resourceFileName.

o bien

resourceFileName es un directorio.

El llamador no dispone del permiso requerido.

Ejemplos

En el ejemplo siguiente se muestra una llamada a DefineUnmanagedResource, pasando un archivo de recursos externo.

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

Comentarios

Un ensamblado solo se puede asociar a un recurso no administrado. Esto significa que la llamada a DefineVersionInfoResource o DefineUnmanagedResource después de llamar a uno de los métodos se llamó anteriormente producirá la excepción System.ArgumentException. Es necesario combinar varios recursos no administrados con una herramienta como la utilidad Microsoft ResMerge (no se proporciona con Common Language Runtime).

Nota

A partir de .NET Framework 2.0 Service Pack 1, este miembro ya no requiere ReflectionPermission con la ReflectionPermissionFlag.ReflectionEmit marca . (Consulte Problemas de seguridad en emisión de reflexión). Para usar esta funcionalidad, la aplicación debe tener como destino .NET Framework 3.5 o posterior.

Se aplica a

DefineUnmanagedResource(Byte[])

Define un recurso no administrado para este ensamblado como blob opaco de bytes.

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

Parámetros

resource
Byte[]

Blob opaco de bytes que representa el recurso no administrado.

Excepciones

Se definió anteriormente un recurso no administrado.

resource es null.

El llamador no dispone del permiso requerido.

Ejemplos

En el ejemplo de código siguiente se crea y se adjunta una matriz de bytes que representa un recurso no administrado a un ensamblado dinámico, mediante 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

Comentarios

Un ensamblado solo se puede asociar a un recurso no administrado. Esto significa que llamar a DefineVersionInfoResource o DefineUnmanagedResource después de llamar a uno de los métodos anteriormente iniciará la excepción System.ArgumentException que se está iniciando. Es necesario combinar varios recursos no administrados con una herramienta como la utilidad Microsoft ResMerge (no se proporciona con Common Language Runtime).

Se aplica a