Compartir a través de


ModuleBuilder.DefineResource Método

Definición

Define un recurso incrustado administrado que se va a almacenar en este módulo.

Sobrecargas

DefineResource(String, String)

Define el recurso incrustado administrado con nombre que se va a almacenar en este módulo.

DefineResource(String, String, ResourceAttributes)

Define el recurso incrustado administrado con nombre y los atributos especificados, que se va a almacenar en este módulo.

DefineResource(String, String)

Define el recurso incrustado administrado con nombre que se va a almacenar en este módulo.

public:
 System::Resources::IResourceWriter ^ DefineResource(System::String ^ name, System::String ^ description);
public System.Resources.IResourceWriter DefineResource (string name, string description);
member this.DefineResource : string * string -> System.Resources.IResourceWriter
Public Function DefineResource (name As String, description As String) As IResourceWriter

Parámetros

name
String

Nombre del recurso. name no puede contener valores null insertados.

description
String

Descripción del recurso.

Devoluciones

Sistema de escritura de recursos para el recurso definido.

Excepciones

La longitud de name es cero.

name es null.

Este módulo es transitorio.

o bien

El ensamblado contenedor no tiene persistencia.

Ejemplos

En el ejemplo siguiente se muestra el uso de DefineResource para agregar un recurso externo al objeto actual ModuleBuilder.

using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
using namespace System::Resources;
public ref class CodeGenerator
{
public:
   CodeGenerator()
   {
      
      // Get the current application domain for the current thread.
      AppDomain^ currentDomain = AppDomain::CurrentDomain;
      AssemblyName^ myAssemblyName = gcnew AssemblyName;
      myAssemblyName->Name = "TempAssembly";
      
      // Define 'TempAssembly' assembly in the current application domain.
      AssemblyBuilder^ myAssemblyBuilder = currentDomain->DefineDynamicAssembly( myAssemblyName, AssemblyBuilderAccess::RunAndSave );
      
      // Define 'TempModule' module in 'TempAssembly' assembly.
      ModuleBuilder^ myModuleBuilder = myAssemblyBuilder->DefineDynamicModule( "TempModule", "TempModule.netmodule", true );
      
      // Define the managed embedded resource, 'MyResource' in 'TempModule'.
      IResourceWriter^ myResourceWriter = myModuleBuilder->DefineResource( "MyResource.resource", "Description" );
      
      // Add resources to the resource writer.
      myResourceWriter->AddResource( "String 1", "First String" );
      myResourceWriter->AddResource( "String 2", "Second String" );
      myResourceWriter->AddResource( "String 3", "Third String" );
      myAssemblyBuilder->Save( "MyAssembly.dll" );
   }

};

int main()
{
   CodeGenerator^ myGenerator = gcnew CodeGenerator;
   Console::WriteLine( "A resource named 'MyResource.resource' has been created and can be viewed in the 'MyAssembly.dll'" );
}
using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Resources;

   public class CodeGenerator
   {
      public CodeGenerator()
      {
         // Get the current application domain for the current thread.
         AppDomain currentDomain = AppDomain.CurrentDomain;

         AssemblyName myAssemblyName = new AssemblyName();
         myAssemblyName.Name = "TempAssembly";

         // Define 'TempAssembly' assembly in the current application domain.
         AssemblyBuilder myAssemblyBuilder =
            currentDomain.DefineDynamicAssembly
                        (myAssemblyName, AssemblyBuilderAccess.RunAndSave);
         // Define 'TempModule' module in 'TempAssembly' assembly.
         ModuleBuilder myModuleBuilder =
            myAssemblyBuilder.DefineDynamicModule("TempModule",
                                       "TempModule.netmodule",true);
         // Define the managed embedded resource, 'MyResource' in 'TempModule'.
         IResourceWriter myResourceWriter =
               myModuleBuilder.DefineResource("MyResource.resource","Description");
         // Add resources to the resource writer.
         myResourceWriter.AddResource("String 1", "First String");
         myResourceWriter.AddResource("String 2", "Second String");
         myResourceWriter.AddResource("String 3", "Third String");
         myAssemblyBuilder.Save("MyAssembly.dll");
      }
   }

   public class CallerClass
   {
      public static void Main()
      {
         CodeGenerator myGenerator = new CodeGenerator();
         Console.WriteLine("A resource named 'MyResource.resource'"
                +" has been created and can be viewed  in the 'MyAssembly.dll'");
      }
   }
Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Resources

Public Class CodeGenerator
   
   Public Sub New()
      ' Get the current application domain for the current thread.
      Dim currentDomain As AppDomain = AppDomain.CurrentDomain

      Dim myAssemblyName As New AssemblyName()
      myAssemblyName.Name = "TempAssembly"
      
      ' Define 'TempAssembly' assembly in the current application domain.
      Dim myAssemblyBuilder As AssemblyBuilder = currentDomain.DefineDynamicAssembly _
                                       (myAssemblyName, AssemblyBuilderAccess.RunAndSave)
      ' Define 'TempModule' module in 'TempAssembly' assembly.
      Dim myModuleBuilder As ModuleBuilder = myAssemblyBuilder.DefineDynamicModule _
                                             ("TempModule", "TempModule.netmodule", True)
      ' Define the managed embedded resource, 'MyResource' in 'TempModule'.
      Dim myResourceWriter As IResourceWriter = myModuleBuilder.DefineResource _
                                                   ("MyResource.resource", "Description")
      ' Add resources to the resource writer.
      myResourceWriter.AddResource("String 1", "First String")
      myResourceWriter.AddResource("String 2", "Second String")
      myResourceWriter.AddResource("String 3", "Third String")
      myAssemblyBuilder.Save("MyAssembly.dll")

   End Sub
End Class

Public Class CallerClass
   
   Public Shared Sub Main()
      Dim myGenerator As New CodeGenerator()
      Console.WriteLine("A resource named 'MyResource.resource' has been created and can be" + _
                                                               " viewed  in the 'MyAssembly.dll'")
   End Sub
End Class

Comentarios

El llamador no debe llamar a los ResourceWriter.Generate() métodos y ResourceWriter.Close() , ya que estos métodos se llaman ModuleBuilder.Save cuando el ensamblado dinámico se escribe en el disco.

Use este método para insertar un recurso administrado. Para insertar un blob de recursos de manifiesto, use el DefineManifestResource método . Para obtener un resumen de la inserción y vinculación de recursos administrados y blobs de recursos de manifiesto, consulte el DefineManifestResource método .

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.

Consulte también

Se aplica a

DefineResource(String, String, ResourceAttributes)

Define el recurso incrustado administrado con nombre y los atributos especificados, que se va a almacenar en este módulo.

public:
 System::Resources::IResourceWriter ^ DefineResource(System::String ^ name, System::String ^ description, System::Reflection::ResourceAttributes attribute);
public System.Resources.IResourceWriter DefineResource (string name, string description, System.Reflection.ResourceAttributes attribute);
member this.DefineResource : string * string * System.Reflection.ResourceAttributes -> System.Resources.IResourceWriter
Public Function DefineResource (name As String, description As String, attribute As ResourceAttributes) As IResourceWriter

Parámetros

name
String

Nombre del recurso. name no puede contener valores null insertados.

description
String

Descripción del recurso.

attribute
ResourceAttributes

Atributos de recursos.

Devoluciones

Sistema de escritura de recursos para el recurso definido.

Excepciones

La longitud de name es cero.

name es null.

Este módulo es transitorio.

o bien

El ensamblado contenedor no tiene persistencia.

Ejemplos

En el ejemplo siguiente se muestra el uso de DefineResource para agregar un recurso externo al objeto actual ModuleBuilder.

using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
using namespace System::Resources;
public ref class CodeGenerator
{
public:
   CodeGenerator()
   {
      
      // Get the current application domain for the current thread.
      AppDomain^ currentDomain = AppDomain::CurrentDomain;
      AssemblyName^ myAssemblyName = gcnew AssemblyName;
      myAssemblyName->Name = "TempAssembly";
      
      // Define 'TempAssembly' assembly in the current application domain.
      AssemblyBuilder^ myAssemblyBuilder = currentDomain->DefineDynamicAssembly( myAssemblyName, AssemblyBuilderAccess::RunAndSave );
      
      // Define 'TempModule' module in 'TempAssembly' assembly.
      ModuleBuilder^ myModuleBuilder = myAssemblyBuilder->DefineDynamicModule( "TempModule", "TempModule.netmodule", true );
      
      // Define the managed embedded resource, 'MyResource' in 'TempModule'
      // with the specified attribute.
      IResourceWriter^ writer = myModuleBuilder->DefineResource( "MyResource.resource", "Description", ResourceAttributes::Public );
      
      // Add resources to the resource writer.
      writer->AddResource( "String 1", "First String" );
      writer->AddResource( "String 2", "Second String" );
      writer->AddResource( "String 3", "Third String" );
      myAssemblyBuilder->Save( "MyAssembly.dll" );
   }

};

int main()
{
   CodeGenerator^ myGenerator = gcnew CodeGenerator;
   Console::WriteLine( "A resource named 'MyResource::resource' has been created and can be viewed in the 'MyAssembly.dll'" );
}
using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Resources;

   public class CodeGenerator
   {
      public CodeGenerator()
      {
         // Get the current application domain for the current thread.
         AppDomain currentDomain = AppDomain.CurrentDomain;

         AssemblyName myAssemblyName = new AssemblyName();
         myAssemblyName.Name = "TempAssembly";

         // Define 'TempAssembly' assembly in the current application domain.
         AssemblyBuilder myAssemblyBuilder =
            currentDomain.DefineDynamicAssembly
                        (myAssemblyName, AssemblyBuilderAccess.RunAndSave);
         // Define 'TempModule' module in 'TempAssembly' assembly.
         ModuleBuilder myModuleBuilder =
            myAssemblyBuilder.DefineDynamicModule("TempModule",
                                              "TempModule.netmodule",true);
         // Define the managed embedded resource, 'MyResource' in 'TempModule'
         // with the specified attribute.
         IResourceWriter writer =
               myModuleBuilder.DefineResource("MyResource.resource",
                            "Description",ResourceAttributes.Public);
         // Add resources to the resource writer.
         writer.AddResource("String 1", "First String");
         writer.AddResource("String 2", "Second String");
         writer.AddResource("String 3", "Third String");
         myAssemblyBuilder.Save("MyAssembly.dll");
      }
   }

   public class CallerClass
   {
      public static void Main()
      {
         CodeGenerator myGenerator = new CodeGenerator();
         Console.WriteLine("A resource named 'MyResource.resource'"
                +" has been created and can be viewed  in the 'MyAssembly.dll'");
      }
   }
Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Resources

Public Class CodeGenerator
   
   Public Sub New()
      ' Get the current application domain for the current thread.
      Dim currentDomain As AppDomain = AppDomain.CurrentDomain

      Dim myAssemblyName As New AssemblyName()
      myAssemblyName.Name = "TempAssembly"
      
      ' Define 'TempAssembly' assembly in the current application domain.
      Dim myAssemblyBuilder As AssemblyBuilder = currentDomain.DefineDynamicAssembly _
                                          (myAssemblyName, AssemblyBuilderAccess.RunAndSave)
      ' Define 'TempModule' module in 'TempAssembly' assembly.
      Dim myModuleBuilder As ModuleBuilder = myAssemblyBuilder.DefineDynamicModule _
                                                ("TempModule", "TempModule.netmodule", True)
      ' Define the managed embedded resource, 'MyResource' in 'TempModule'
      ' with the specified attribute.
      Dim writer As IResourceWriter = myModuleBuilder.DefineResource _
                              ("MyResource.resource", "Description", ResourceAttributes.Public)
      ' Add resources to the resource writer.
      writer.AddResource("String 1", "First String")
      writer.AddResource("String 2", "Second String")
      writer.AddResource("String 3", "Third String")
      myAssemblyBuilder.Save("MyAssembly.dll")

   End Sub
End Class

Public Class CallerClass
   
   Public Shared Sub Main()
      Dim myGenerator As New CodeGenerator()
      Console.WriteLine("A resource named 'MyResource.resource' has been created and can be " + _
                                                         "viewed  in the 'MyAssembly.dll'")
   End Sub
End Class

Comentarios

El llamador no debe llamar a los ResourceWriter.Generate() métodos y ResourceWriter.Close() , ya que estos métodos se llaman ModuleBuilder.Save cuando el ensamblado dinámico se escribe en el disco.

Use este método para insertar un recurso administrado. Para insertar un blob de recursos de manifiesto, use el DefineManifestResource método . Para obtener un resumen de la inserción y vinculación de recursos administrados y blobs de recursos de manifiesto, consulte el DefineManifestResource método .

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.

Consulte también

Se aplica a