AppDomain.DefineDynamicAssembly Method (AssemblyName, AssemblyBuilderAccess, Evidence, PermissionSet, PermissionSet, PermissionSet)
Assembly: mscorlib (in mscorlib.dll)
public AssemblyBuilder DefineDynamicAssembly (
AssemblyName name,
AssemblyBuilderAccess access,
Evidence evidence,
PermissionSet requiredPermissions,
PermissionSet optionalPermissions,
PermissionSet refusedPermissions
)
public final AssemblyBuilder DefineDynamicAssembly ( AssemblyName name, AssemblyBuilderAccess access, Evidence evidence, PermissionSet requiredPermissions, PermissionSet optionalPermissions, PermissionSet refusedPermissions )
public final function DefineDynamicAssembly ( name : AssemblyName, access : AssemblyBuilderAccess, evidence : Evidence, requiredPermissions : PermissionSet, optionalPermissions : PermissionSet, refusedPermissions : PermissionSet ) : AssemblyBuilder
Parameters
- name
The unique identity of the dynamic assembly.
- access
The mode in which the dynamic assembly will be accessed.
- evidence
The evidence supplied for the dynamic assembly. The evidence is used unaltered as the final set of evidence used for policy resolution.
- requiredPermissions
The required permissions request.
- optionalPermissions
The optional permissions request.
- refusedPermissions
The refused permissions request.
Return Value
Represents the dynamic assembly created.| Exception type | Condition |
|---|---|
| name is a null reference (Nothing in Visual Basic). | |
| The Name property of name is a null reference (Nothing in Visual Basic). -or- The Name property of name begins with white space, or contains a forward or backward slash. | |
| Operations are attempted on an unloaded application domain. |
Only fully trusted callers can supply their evidence when defining a dynamic Assembly. The runtime will map the Evidence through the security policy to determine the granted permissions. Partially trusted callers must supply a null evidence. If evidence is a null reference (Nothing in Visual Basic), the runtime copies the permission sets, that is, the current grant and deny sets, from the caller's Assembly to the dynamic Assembly being defined and marks policy as resolved.
If the dynamic Assembly is saved to disk, subsequent loads will get grants based on policies associated with the location where the Assembly was saved.
This method should only be used to define a dynamic assembly in the current application domain. For more information, see the Load(AssemblyName) method overload.
The name parameter indirectly specifies a Version (as an OperatingSystem constructor parameter). However, this method only persists the major and minor version numbers, not the build and revision numbers. For example, if the LoadFrom method is used to indirectly recover the Version for this assembly, only that object's major and minor version numbers will be as originally specified.
The following sample demonstrates the DefineDynamicAssembly method and 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; } }
- FileIOPermission for access to read from a file or directory, and for access to the information in the path itself. Associated enumerations: FileIOPermissionAccess.Read, FileIOPermissionAccess.PathDiscovery.
- SecurityPermission to load an assembly with evidence. Associated enumeration: SecurityPermissionFlag.ControlEvidence.
Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.