Este artículo se tradujo automáticamente. Para ver el artículo en inglés, active la casilla Inglés. Además, puede mostrar el texto en inglés en una ventana emergente si mueve el puntero del mouse sobre el texto.
Traducción
Inglés

Método AppDomain.DefineDynamicAssembly (AssemblyName, AssemblyBuilderAccess, String, PermissionSet, PermissionSet, PermissionSet)

 

Publicado: octubre de 2016

obsoleteCodeEntityM:System.AppDomain.DefineDynamicAssembly(System.Reflection.AssemblyName,System.Reflection.Emit.AssemblyBuilderAccess,System.String) Nota: Esta API ahora está obsoleta.

Define un ensamblado dinámico usando el nombre especificado, el modo de acceso, el directorio de almacenamiento y las solicitudes de permiso.

Espacio de nombres:   System
Ensamblado:  mscorlib (en mscorlib.dll)

[ObsoleteAttribute("Assembly level declarative security is obsolete and is no longer enforced by the CLR by default. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")]
public AssemblyBuilder DefineDynamicAssembly(
	AssemblyName name,
	AssemblyBuilderAccess access,
	string dir,
	PermissionSet requiredPermissions,
	PermissionSet optionalPermissions,
	PermissionSet refusedPermissions
)

Parámetros

name
Type: System.Reflection.AssemblyName

Identidad única del ensamblado dinámico.

access
Type: System.Reflection.Emit.AssemblyBuilderAccess

Modo de acceso al ensamblado dinámico.

dir
Type: System.String

Nombre del directorio donde se guardará el ensamblado. Si dir es null, el directorio predeterminado es el directorio actual.

requiredPermissions
Type: System.Security.PermissionSet

Solicitud de permisos requeridos.

optionalPermissions
Type: System.Security.PermissionSet

Solicitud de permisos opcionales.

refusedPermissions
Type: System.Security.PermissionSet

Solicitud de permisos rechazados.

Valor devuelto

Type: System.Reflection.Emit.AssemblyBuilder

Ensamblado dinámico con el nombre y las características especificados.

Exception Condition
ArgumentNullException

El valor de name es null.

ArgumentException

La propiedad Name de name es null.

O bien

La propiedad Name de name empieza con un espacio en blanco o contiene una barra diagonal o una barra diagonal inversa.

AppDomainUnloadedException

La operación se intenta en un dominio de aplicación descargado.

The permission requests specified for requiredPermissions, optionalPermissions, and refusedPermissions are not used unless the dynamic assembly has been saved and reloaded into memory. To specify permission requests for a transient assembly that is never saved to disk, use an overload of the Overload:System.AppDomain.DefineDynamicAssembly method that specifies evidence as well as requested permissions, and supply an T:System.Security.Policy.Evidence object.

System_CAPS_noteNota

During the development of code that emits dynamic assemblies, it is recommended that you use an overload of the Overload:System.AppDomain.DefineDynamicAssembly method that specifies evidence and permissions, supply the evidence you want the dynamic assembly to have, and include F:System.Security.Permissions.SecurityPermissionFlag.SkipVerification in refusedPermissions. Including F:System.Security.Permissions.SecurityPermissionFlag.SkipVerification in the refusedPermissions parameter ensures that the MSIL is verified. A limitation of this technique is that it also causes T:System.Security.SecurityException to be thrown when used with code that demands full trust.

This method should only be used to define a dynamic assembly in the current application domain. For more information, see the M:System.AppDomain.Load(System.Reflection.AssemblyName) method overload.

The following sample demonstrates the M:System.AppDomain.DefineDynamicAssembly(System.Reflection.AssemblyName,System.Reflection.Emit.AssemblyBuilderAccess) method and E:System.AppDomain.AssemblyResolve event.

For this code example to run, you must provide the fully qualified assembly name. For information about how to obtain the fully qualified assembly name, see Assembly Names.

using System;
using System.Reflection;
using System.Reflection.Emit;

class Test {
   public static void Main() {
      AppDomain currentDomain = AppDomain.CurrentDomain;

      InstantiateMyDynamicType(currentDomain);   // Failed!

      currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);

      InstantiateMyDynamicType(currentDomain);   // OK!
   }

   static void InstantiateMyDynamicType(AppDomain domain) {
      try {
         // You must supply a valid fully qualified assembly name here. 
         domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType");
      } catch (Exception e) {
         Console.WriteLine(e.Message);
      }
   }   

   static Assembly MyResolveEventHandler(object sender, ResolveEventArgs args) {
      return DefineDynamicAssembly((AppDomain) sender);
   }

   static Assembly DefineDynamicAssembly(AppDomain domain) {
      // Build a dynamic assembly using Reflection Emit API.

      AssemblyName assemblyName = new AssemblyName();
      assemblyName.Name = "MyDynamicAssembly";

      AssemblyBuilder assemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
      ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("MyDynamicModule");
      TypeBuilder typeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public);
      ConstructorBuilder constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, null);
      ILGenerator ilGenerator = constructorBuilder.GetILGenerator();

      ilGenerator.EmitWriteLine("MyDynamicType instantiated!");
      ilGenerator.Emit(OpCodes.Ret);

      typeBuilder.CreateType();

      return assemblyBuilder;
   }
}

.NET Framework
Disponible desde 1.1
Volver al principio
Mostrar: