AppDomain.DefineDynamicAssembly Method (AssemblyName, AssemblyBuilderAccess, Evidence)
Assembly: mscorlib (in mscorlib.dll)
'Declaration Public Function DefineDynamicAssembly ( _ name As AssemblyName, _ access As AssemblyBuilderAccess, _ evidence As Evidence _ ) As AssemblyBuilder 'Usage Dim instance As AppDomain Dim name As AssemblyName Dim access As AssemblyBuilderAccess Dim evidence As Evidence Dim returnValue As AssemblyBuilder returnValue = instance.DefineDynamicAssembly(name, access, evidence)
public final AssemblyBuilder DefineDynamicAssembly ( AssemblyName name, AssemblyBuilderAccess access, Evidence evidence )
public final function DefineDynamicAssembly ( name : AssemblyName, access : AssemblyBuilderAccess, evidence : Evidence ) : AssemblyBuilder
The unique identity of the dynamic assembly.
The mode in which the dynamic assembly will be accessed.
The evidence supplied for the dynamic assembly. The evidence is used unaltered as the final set of evidence used for policy resolution.
Return ValueRepresents the dynamic assembly created.
name is a null reference (Nothing in Visual Basic).
The Name property of name is a null reference (Nothing in Visual Basic).
The Name property of name begins with white space, or contains a forward or backward slash.
The operation is 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.
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.
First, the code example tries to create an instance of MyDynamicType by calling the CreateInstance method with an invalid assembly name, and catches the resulting exception.
The code example then adds an event handler for the AssemblyResolve event, and again tries to create an instance of MyDynamicType. During the call to CreateInstance, the AssemblyResolve event is raised for the invalid assembly. The event handler creates a dynamic assembly that contains a type named MyDynamicType, gives the type a parameterless constructor, and returns the new dynamic assembly. The call to CreateInstance then finishes successfully, and the constructor for MyDynamicType displays a message at the console.
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
Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter EditionThe Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.