Questo articolo è stato tradotto automaticamente. Per visualizzare l'articolo in inglese, selezionare la casella di controllo Inglese. È possibile anche visualizzare il testo inglese in una finestra popup posizionando il puntatore del mouse sopra il testo.
Traduzione
Inglese

Metodo AppDomain.DefineDynamicAssembly (AssemblyName, AssemblyBuilderAccess, String, Evidence)

 

Data di pubblicazione: ottobre 2016

obsoleteCodeEntityM:System.AppDomain.DefineDynamicAssembly(System.Reflection.AssemblyName,System.Reflection.Emit.AssemblyBuilderAccess,System.String) Nota: Questa API è ora obsoleta.

Definisce un assembly dinamico mediante il nome, la modalità di accesso, la directory di archiviazione e l'evidenza specificati.

Spazio dei nomi:   System
Assembly:  mscorlib (in mscorlib.dll)

[ObsoleteAttribute("Methods which use evidence to sandbox are obsolete and will be removed in a future release of the .NET Framework. Please use an overload of DefineDynamicAssembly which does not take an Evidence parameter. See http://go.microsoft.com/fwlink/?LinkId=155570 for more information.")]
public AssemblyBuilder DefineDynamicAssembly(
	AssemblyName name,
	AssemblyBuilderAccess access,
	string dir,
	Evidence evidence
)

Parametri

name
Type: System.Reflection.AssemblyName

Identità univoca dell'assembly dinamico.

access
Type: System.Reflection.Emit.AssemblyBuilderAccess

Modalità mediante cui si accederà all'assembly dinamico.

dir
Type: System.String

Nome della directory nella quale verrà salvato l'assembly. Se dir è null, la directory predefinita sarà la directory corrente.

evidence
Type: System.Security.Policy.Evidence

Evidenza fornita per l'assembly dinamico. L'evidenza viene usata senza modifiche come il set di evidenza finale impiegato per la risoluzione dei criteri.

Valore restituito

Type: System.Reflection.Emit.AssemblyBuilder

Assembly dinamico con nome e funzionalità specificati.

Exception Condition
ArgumentNullException

name è null.

ArgumentException

La proprietà Name di name è null.

-oppure-

La proprietà Name di name inizia con uno spazio vuoto o contiene una barra o una barra rovesciata.

AppDomainUnloadedException

L'operazione viene tentata in un dominio dell'applicazione non caricato.

Only fully trusted callers can supply their evidence when defining a dynamic T:System.Reflection.Assembly. The runtime will map the T:System.Security.Policy.Evidence through the security policy to determine the granted permissions. Partially trusted callers must supply a null evidence. If evidence is null, the runtime copies the permission sets, that is, the current grant and deny sets, from the caller's T:System.Reflection.Assembly to the dynamic T:System.Reflection.Assembly being defined and marks policy as resolved.

If the dynamic T:System.Reflection.Assembly is saved to disk, subsequent loads will get grants based on policies associated with the location where the T:System.Reflection.Assembly was saved.

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.

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.

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;
   }
}

SecurityPermission

to load an assembly with evidence. Associated enumeration: F:System.Security.Permissions.SecurityPermissionFlag.ControlEvidence.

.NET Framework
Disponibile da 1.1
Torna all'inizio
Mostra: