DefineDynamicAssembly Method (AssemblyName, AssemblyBuilderAccess, String, Evidence, PermissionSet, PermissionSet, PermissionSet, Boolean)
Collapse the table of content
Expand the table of content

AppDomain.DefineDynamicAssembly Method (AssemblyName, AssemblyBuilderAccess, String, Evidence, PermissionSet, PermissionSet, PermissionSet, Boolean)

 
obsoleteCodeEntityM:System.AppDomain.DefineDynamicAssembly(System.Reflection.AssemblyName,System.Reflection.Emit.AssemblyBuilderAccess,System.String,System.Boolean,System.Collections.Generic.IEnumerable{System.Reflection.Emit.CustomAttributeBuilder})Note: This API is now obsolete.

Defines a dynamic assembly using the specified name, access mode, storage directory, evidence, permission requests, and synchronization option.

Namespace:   System
Assembly:  mscorlib (in 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 Function DefineDynamicAssembly (
	name As AssemblyName,
	access As AssemblyBuilderAccess,
	dir As String,
	evidence As Evidence,
	requiredPermissions As PermissionSet,
	optionalPermissions As PermissionSet,
	refusedPermissions As PermissionSet,
	isSynchronized As Boolean
) As AssemblyBuilder

Parameters

name
Type: System.Reflection.AssemblyName

The unique identity of the dynamic assembly.

access
Type: System.Reflection.Emit.AssemblyBuilderAccess

The mode in which the dynamic assembly will be accessed.

dir
Type: System.String

The name of the directory where the dynamic assembly will be saved. If dir is null, the directory defaults to the current directory.

evidence
Type: System.Security.Policy.Evidence

The evidence supplied for the dynamic assembly. The evidence is used unaltered as the final set of evidence used for policy resolution.

requiredPermissions
Type: System.Security.PermissionSet

The required permissions request.

optionalPermissions
Type: System.Security.PermissionSet

The optional permissions request.

refusedPermissions
Type: System.Security.PermissionSet

The refused permissions request.

isSynchronized
Type: System.Boolean

true to synchronize the creation of modules, types, and members in the dynamic assembly; otherwise, false.

Return Value

Type: System.Reflection.Emit.AssemblyBuilder

A dynamic assembly with the specified name and features.

Exception Condition
ArgumentNullException

name is null.

ArgumentException

The Name property of name is null.

-or-

The Name property of name begins with white space, or contains a forward or backward slash.

AppDomainUnloadedException

The operation is attempted on an unloaded application domain.

The permission requests specified for requiredPermissions, optionalPermissions, and refusedPermissions are used only if evidence is also supplied, or if the dynamic assembly is saved and reloaded into memory.

System_CAPS_noteNote

During the development of code that emits dynamic assemblies, it is recommended that you include SecurityPermissionFlag.SkipVerification in refusedPermissions. Including SkipVerification in the refusedPermissions parameter ensures that the MSIL is verified. A limitation of this technique is that it also causes SecurityException to be thrown when used with code that demands full trust.

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 null for the evidence parameter. If evidence is null, 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.

If isSynchronized is true, the following methods of the resulting AssemblyBuilder will be synchronized: DefineDynamicModule, DefineResource, AddResourceFile, GetDynamicModule, SetEntryPoint, and Save. If two of these methods are called on different threads, one will block until the other completes.

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.


Imports System
Imports System.Reflection
Imports System.Reflection.Emit

Module Test

   Sub Main()
      Dim currentDomain As AppDomain = AppDomain.CurrentDomain

      InstantiateMyDynamicType(currentDomain)   'Failed!

      AddHandler currentDomain.AssemblyResolve, AddressOf MyResolveEventHandler

      InstantiateMyDynamicType(currentDomain)   'OK!
   End Sub 'Main

   Sub InstantiateMyDynamicType(domain As AppDomain)
      Try
         ' You must supply a valid fully qualified assembly name here.
         domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType")
      Catch e As Exception
         Console.WriteLine(e.Message)
      End Try
   End Sub 'InstantiateMyDynamicType

   Function MyResolveEventHandler(sender As Object, args As ResolveEventArgs) As System.Reflection.Assembly
      Return DefineDynamicAssembly(DirectCast(sender, AppDomain))
   End Function 'MyResolveEventHandler

   Function DefineDynamicAssembly(domain As AppDomain) As System.Reflection.Assembly
      ' Build a dynamic assembly using Reflection Emit API.

      Dim assemblyName As New AssemblyName()
      assemblyName.Name = "MyDynamicAssembly"

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

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

      typeBuilder.CreateType()

      Return assemblyBuilder
   End Function 'DefineDynamicAssembly

End Module 'Test 

SecurityPermission

to load an assembly with evidence. Associated enumeration: SecurityPermissionFlag.ControlEvidence.

.NET Framework
Available since 1.1
Return to top
Show:
© 2016 Microsoft