Expand Minimize
This topic has not yet been rated - Rate this topic

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

Note: This API is now obsolete. The non-obsolete alternative is DefineDynamicAssembly.

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

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
'Declaration
<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, _
	requiredPermissions As PermissionSet, _
	optionalPermissions As PermissionSet, _
	refusedPermissions As PermissionSet _
) 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 assembly will be saved. If dir is Nothing, the directory defaults to the current directory.

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.

Return Value

Type: System.Reflection.Emit.AssemblyBuilder
A dynamic assembly with the specified name and features.

Implements

_AppDomain.DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, PermissionSet, PermissionSet, PermissionSet)
ExceptionCondition
ArgumentNullException

name is Nothing.

ArgumentException

The Name property of name is Nothing.

-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 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 DefineDynamicAssembly method that specifies evidence as well as requested permissions, and supply an Evidence object.

NoteNote

During the development of code that emits dynamic assemblies, it is recommended that you use an overload of the DefineDynamicAssembly method that specifies evidence and permissions, supply the evidence you want the dynamic assembly to have, and 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.

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.

NoteNote

In the .NET Framework versions 1.0, 1.1, and 2.0, the declarative security attributes applied to a dynamic assembly by using the requiredPermissions, optionalPermissions, and refusedPermissions parameters are stored in the old XML metadata format. See Emitting Declarative Security Attributes.

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 

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
Obsolete (compiler warning) in 4.5.1
Obsolete (compiler warning) in 4.5
Obsolete (compiler warning) in 4

.NET Framework Client Profile

Supported in: 3.5 SP1
Obsolete (compiler warning) in 4

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.