AppDomain.DefineDynamicAssembly 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
在当前应用程序域中定义动态程序集。
重载
DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, Evidence, PermissionSet, PermissionSet, PermissionSet, Boolean, IEnumerable<CustomAttributeBuilder>)
注意
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:
System::Reflection::Emit::AssemblyBuilder ^ DefineDynamicAssembly(System::Reflection::AssemblyName ^ name, System::Reflection::Emit::AssemblyBuilderAccess access, System::String ^ dir, System::Security::Policy::Evidence ^ evidence, System::Security::PermissionSet ^ requiredPermissions, System::Security::PermissionSet ^ optionalPermissions, System::Security::PermissionSet ^ refusedPermissions, bool isSynchronized, System::Collections::Generic::IEnumerable<System::Reflection::Emit::CustomAttributeBuilder ^> ^ assemblyAttributes);
public System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, string dir, System.Security.Policy.Evidence evidence, System.Security.PermissionSet requiredPermissions, System.Security.PermissionSet optionalPermissions, System.Security.PermissionSet refusedPermissions, bool isSynchronized, System.Collections.Generic.IEnumerable<System.Reflection.Emit.CustomAttributeBuilder> assemblyAttributes);
[System.Obsolete("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 System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, string dir, System.Security.Policy.Evidence evidence, System.Security.PermissionSet requiredPermissions, System.Security.PermissionSet optionalPermissions, System.Security.PermissionSet refusedPermissions, bool isSynchronized, System.Collections.Generic.IEnumerable<System.Reflection.Emit.CustomAttributeBuilder> assemblyAttributes);
member this.DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * string * System.Security.Policy.Evidence * System.Security.PermissionSet * System.Security.PermissionSet * System.Security.PermissionSet * bool * seq<System.Reflection.Emit.CustomAttributeBuilder> -> System.Reflection.Emit.AssemblyBuilder
[<System.Obsolete("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.")>]
member this.DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * string * System.Security.Policy.Evidence * System.Security.PermissionSet * System.Security.PermissionSet * System.Security.PermissionSet * bool * seq<System.Reflection.Emit.CustomAttributeBuilder> -> System.Reflection.Emit.AssemblyBuilder
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, assemblyAttributes As IEnumerable(Of CustomAttributeBuilder)) As AssemblyBuilder
参数
- name
- AssemblyName
动态程序集的唯一标识。
- access
- AssemblyBuilderAccess
访问动态程序集所采用的模式。
- dir
- String
保存动态程序集的目录的名称。 如果 dir
为 null
,则使用当前目录。
- evidence
- Evidence
为动态程序集提供的证据。 该证据始终作为最后一组用于策略解析的证据来使用。
- requiredPermissions
- PermissionSet
必需的权限请求。
- optionalPermissions
- PermissionSet
可选的权限请求。
- refusedPermissions
- PermissionSet
被拒绝的权限请求。
- isSynchronized
- Boolean
若要在动态程序集中同步模块、类型和成员的创建,则为 true
;否则为 false
。
- assemblyAttributes
- IEnumerable<CustomAttributeBuilder>
要应用于程序集的可枚举特性列表;如果无特性,则为 null
。
返回
具有指定名称和功能的动态程序集。
- 属性
例外
name
为 null
。
在卸载的应用程序域上尝试该操作。
注解
使用此方法重载指定无法正常工作的属性,除非在创建动态程序集时应用这些属性。 例如,如果在创建动态程序集后添加安全属性(如 SecurityTransparentAttribute 和 SecurityCriticalAttribute ),则它们无法正常工作。
仅当evidence
同时提供了 参数,或者保存动态程序集并将其重新加载到内存中时,才使用为 requiredPermissions
、 optionalPermissions
和 refusedPermissions
参数指定的权限请求。
注意
开发发出动态程序集的代码时,建议在 SecurityPermissionFlag.SkipVerification 参数中包含 refusedPermissions
标志。 包含此标志可确保验证 Microsoft 中间语言 (MSIL) 。 此方法将检测意外生成的不可验证代码,否则很难检测。 此方法的一个限制是,当与需要完全信任的代码一起使用时,它也会导致 SecurityException 引发。
只有完全信任的调用方才能在定义动态 Assembly时提供证据。 运行时通过安全策略映射 Evidence ,以确定授予的权限。 部分受信任的调用方必须为 参数提供 null
evidence
。 如果 evidence
为 null
,则运行时复制权限集 (即,当前授予和拒绝将) 从调用方程序集设置为正在定义的动态程序集,并将策略标记为已解决。
如果将动态程序集保存到磁盘,则后续加载将根据与动态程序集保存位置关联的策略获得授权。
如果 isSynchronized
为 true
,则生成的 AssemblyBuilder 的以下方法将同步:DefineDynamicModule、、DefineResource、AddResourceFileGetDynamicModule、 SetEntryPoint和 Save。 如果在不同的线程上调用其中两个方法,则一个方法将阻塞,直到另一个方法完成。
此方法重载在 .NET Framework 3.5 中引入。
另请参阅
适用于
DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, Evidence, PermissionSet, PermissionSet, PermissionSet, Boolean)
注意
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:
virtual System::Reflection::Emit::AssemblyBuilder ^ DefineDynamicAssembly(System::Reflection::AssemblyName ^ name, System::Reflection::Emit::AssemblyBuilderAccess access, System::String ^ dir, System::Security::Policy::Evidence ^ evidence, System::Security::PermissionSet ^ requiredPermissions, System::Security::PermissionSet ^ optionalPermissions, System::Security::PermissionSet ^ refusedPermissions, bool isSynchronized);
public System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, string dir, System.Security.Policy.Evidence evidence, System.Security.PermissionSet requiredPermissions, System.Security.PermissionSet optionalPermissions, System.Security.PermissionSet refusedPermissions, bool isSynchronized);
[System.Obsolete("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 System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, string dir, System.Security.Policy.Evidence evidence, System.Security.PermissionSet requiredPermissions, System.Security.PermissionSet optionalPermissions, System.Security.PermissionSet refusedPermissions, bool isSynchronized);
abstract member DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * string * System.Security.Policy.Evidence * System.Security.PermissionSet * System.Security.PermissionSet * System.Security.PermissionSet * bool -> System.Reflection.Emit.AssemblyBuilder
override this.DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * string * System.Security.Policy.Evidence * System.Security.PermissionSet * System.Security.PermissionSet * System.Security.PermissionSet * bool -> System.Reflection.Emit.AssemblyBuilder
[<System.Obsolete("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.")>]
abstract member DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * string * System.Security.Policy.Evidence * System.Security.PermissionSet * System.Security.PermissionSet * System.Security.PermissionSet * bool -> System.Reflection.Emit.AssemblyBuilder
override this.DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * string * System.Security.Policy.Evidence * System.Security.PermissionSet * System.Security.PermissionSet * System.Security.PermissionSet * bool -> System.Reflection.Emit.AssemblyBuilder
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
参数
- name
- AssemblyName
动态程序集的唯一标识。
- access
- AssemblyBuilderAccess
访问动态程序集所采用的模式。
- dir
- String
保存动态程序集的目录的名称。 如果 dir
是 null
,目录将默认为当前目录。
- evidence
- Evidence
为动态程序集提供的证据。 该证据始终作为最后一组用于策略解析的证据来使用。
- requiredPermissions
- PermissionSet
必需的权限请求。
- optionalPermissions
- PermissionSet
可选的权限请求。
- refusedPermissions
- PermissionSet
被拒绝的权限请求。
- isSynchronized
- Boolean
若要在动态程序集中同步模块、类型和成员的创建,则为 true
;否则为 false
。
返回
具有指定名称和功能的动态程序集。
实现
- 属性
例外
name
为 null
。
在卸载的应用程序域上尝试该操作。
示例
下面的示例演示 DefineDynamicAssembly 了 方法和 AssemblyResolve 事件。
若要运行此代码示例,必须提供完全限定的程序集名称。 有关如何获取完全限定程序集名称的信息,请参阅 程序集名称。
using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
ref class Test
{
public:
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( dynamic_cast<AppDomain^>(sender) );
}
static Assembly^ DefineDynamicAssembly( AppDomain^ domain )
{
// Build a dynamic assembly using Reflection Emit API.
AssemblyName^ assemblyName = gcnew 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, nullptr );
ILGenerator^ ilGenerator = constructorBuilder->GetILGenerator();
ilGenerator->EmitWriteLine( "MyDynamicType instantiated!" );
ilGenerator->Emit( OpCodes::Ret );
typeBuilder->CreateType();
return assemblyBuilder;
}
};
int main()
{
AppDomain^ currentDomain = AppDomain::CurrentDomain;
Test::InstantiateMyDynamicType( currentDomain ); // Failed!
currentDomain->AssemblyResolve += gcnew ResolveEventHandler( Test::MyResolveEventHandler );
Test::InstantiateMyDynamicType( currentDomain ); // OK!
}
using System;
using System.Reflection;
using System.Reflection.Emit;
class DefinedDynamicAssemblySnippet {
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;
}
}
open System
open System.Reflection
open System.Reflection.Emit
let instantiateMyDynamicType (domain: AppDomain) =
try
// You must supply a valid fully qualified assembly name here.
domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType")
|> ignore
with e ->
printfn $"{e.Message}"
let defineDynamicAssembly (domain: AppDomain) =
// Build a dynamic assembly using Reflection Emit API.
let assemblyName = AssemblyName()
assemblyName.Name <- "MyDynamicAssembly"
let assemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run)
let moduleBuilder = assemblyBuilder.DefineDynamicModule "MyDynamicModule"
let typeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public)
let constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, null)
let ilGenerator = constructorBuilder.GetILGenerator()
ilGenerator.EmitWriteLine "MyDynamicType instantiated!"
ilGenerator.Emit OpCodes.Ret
typeBuilder.CreateType() |> ignore
assemblyBuilder
let myResolveEventHandler (sender: obj) _ =
defineDynamicAssembly (sender :?> AppDomain)
:> Assembly
let currentDomain = AppDomain.CurrentDomain
instantiateMyDynamicType currentDomain // Failed!
currentDomain.add_AssemblyResolve(ResolveEventHandler myResolveEventHandler)
instantiateMyDynamicType currentDomain // OK!
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
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
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
注解
仅当evidence
同时提供了 ,或者保存动态程序集并将其重新加载到内存中时,才使用为 requiredPermissions
、 optionalPermissions
和 refusedPermissions
指定的权限请求。
注意
在开发发出动态程序集的代码期间,建议将 包含在 SecurityPermissionFlag.SkipVerification 中 refusedPermissions
。 在 SkipVerification 参数中包含 refusedPermissions
可确保验证 MSIL。 此方法的一个限制是,当与需要完全信任的代码一起使用时,它也会导致 SecurityException 引发。
定义动态 Assembly时,只有完全信任的调用方才能提供其证据。 运行时将通过安全策略映射 Evidence ,以确定授予的权限。 部分受信任的调用方必须为 参数提供 null
evidence
。 如果 evidence
为 null
,则运行时会将权限集(即当前授予和拒绝集)从调用者的 Assembly 复制到正在定义的动态 Assembly ,并将策略标记为已解决。
如果将动态 Assembly 保存到磁盘,则后续加载将根据与保存 的位置 Assembly 关联的策略获得授权。
如果 isSynchronized
为 true
,则生成的 AssemblyBuilder 的以下方法将同步:DefineDynamicModule、、DefineResource、AddResourceFileGetDynamicModule、 SetEntryPoint和 Save。 如果在不同的线程上调用其中两个方法,则一个方法将阻塞,直到另一个方法完成。
适用于
DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, Evidence, PermissionSet, PermissionSet, PermissionSet)
注意
Assembly level declarative security is obsolete and is no longer enforced by the CLR by default. Please see http://go.microsoft.com/fwlink/?LinkId=155570 for more information.
使用指定名称、访问模式、存储目录、证据和权限请求定义动态程序集。
public:
virtual System::Reflection::Emit::AssemblyBuilder ^ DefineDynamicAssembly(System::Reflection::AssemblyName ^ name, System::Reflection::Emit::AssemblyBuilderAccess access, System::String ^ dir, System::Security::Policy::Evidence ^ evidence, System::Security::PermissionSet ^ requiredPermissions, System::Security::PermissionSet ^ optionalPermissions, System::Security::PermissionSet ^ refusedPermissions);
public System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, string dir, System.Security.Policy.Evidence evidence, System.Security.PermissionSet requiredPermissions, System.Security.PermissionSet optionalPermissions, System.Security.PermissionSet refusedPermissions);
[System.Obsolete("Assembly level declarative security is obsolete and is no longer enforced by the CLR by default. Please see http://go.microsoft.com/fwlink/?LinkId=155570 for more information.")]
public System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, string dir, System.Security.Policy.Evidence evidence, System.Security.PermissionSet requiredPermissions, System.Security.PermissionSet optionalPermissions, System.Security.PermissionSet refusedPermissions);
abstract member DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * string * System.Security.Policy.Evidence * System.Security.PermissionSet * System.Security.PermissionSet * System.Security.PermissionSet -> System.Reflection.Emit.AssemblyBuilder
override this.DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * string * System.Security.Policy.Evidence * System.Security.PermissionSet * System.Security.PermissionSet * System.Security.PermissionSet -> System.Reflection.Emit.AssemblyBuilder
[<System.Obsolete("Assembly level declarative security is obsolete and is no longer enforced by the CLR by default. Please see http://go.microsoft.com/fwlink/?LinkId=155570 for more information.")>]
abstract member DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * string * System.Security.Policy.Evidence * System.Security.PermissionSet * System.Security.PermissionSet * System.Security.PermissionSet -> System.Reflection.Emit.AssemblyBuilder
override this.DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * string * System.Security.Policy.Evidence * System.Security.PermissionSet * System.Security.PermissionSet * System.Security.PermissionSet -> System.Reflection.Emit.AssemblyBuilder
Public Function DefineDynamicAssembly (name As AssemblyName, access As AssemblyBuilderAccess, dir As String, evidence As Evidence, requiredPermissions As PermissionSet, optionalPermissions As PermissionSet, refusedPermissions As PermissionSet) As AssemblyBuilder
参数
- name
- AssemblyName
动态程序集的唯一标识。
- access
- AssemblyBuilderAccess
访问动态程序集所采用的模式。
- dir
- String
保存程序集的目录的名称。 如果 dir
是 null
,目录将默认为当前目录。
- evidence
- Evidence
为动态程序集提供的证据。 该证据始终作为最后一组用于策略解析的证据来使用。
- requiredPermissions
- PermissionSet
必需的权限请求。
- optionalPermissions
- PermissionSet
可选的权限请求。
- refusedPermissions
- PermissionSet
被拒绝的权限请求。
返回
具有指定名称和功能的动态程序集。
实现
- 属性
例外
name
为 null
。
在卸载的应用程序域上尝试该操作。
示例
下面的示例演示 DefineDynamicAssembly 了 方法和 AssemblyResolve 事件。
若要运行此代码示例,必须提供完全限定的程序集名称。 有关如何获取完全限定程序集名称的信息,请参阅 程序集名称。
using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
ref class Test
{
public:
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( dynamic_cast<AppDomain^>(sender) );
}
static Assembly^ DefineDynamicAssembly( AppDomain^ domain )
{
// Build a dynamic assembly using Reflection Emit API.
AssemblyName^ assemblyName = gcnew 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, nullptr );
ILGenerator^ ilGenerator = constructorBuilder->GetILGenerator();
ilGenerator->EmitWriteLine( "MyDynamicType instantiated!" );
ilGenerator->Emit( OpCodes::Ret );
typeBuilder->CreateType();
return assemblyBuilder;
}
};
int main()
{
AppDomain^ currentDomain = AppDomain::CurrentDomain;
Test::InstantiateMyDynamicType( currentDomain ); // Failed!
currentDomain->AssemblyResolve += gcnew ResolveEventHandler( Test::MyResolveEventHandler );
Test::InstantiateMyDynamicType( currentDomain ); // OK!
}
using System;
using System.Reflection;
using System.Reflection.Emit;
class DefinedDynamicAssemblySnippet {
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;
}
}
open System
open System.Reflection
open System.Reflection.Emit
let instantiateMyDynamicType (domain: AppDomain) =
try
// You must supply a valid fully qualified assembly name here.
domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType")
|> ignore
with e ->
printfn $"{e.Message}"
let defineDynamicAssembly (domain: AppDomain) =
// Build a dynamic assembly using Reflection Emit API.
let assemblyName = AssemblyName()
assemblyName.Name <- "MyDynamicAssembly"
let assemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run)
let moduleBuilder = assemblyBuilder.DefineDynamicModule "MyDynamicModule"
let typeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public)
let constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, null)
let ilGenerator = constructorBuilder.GetILGenerator()
ilGenerator.EmitWriteLine "MyDynamicType instantiated!"
ilGenerator.Emit OpCodes.Ret
typeBuilder.CreateType() |> ignore
assemblyBuilder
let myResolveEventHandler (sender: obj) _ =
defineDynamicAssembly (sender :?> AppDomain)
:> Assembly
let currentDomain = AppDomain.CurrentDomain
instantiateMyDynamicType currentDomain // Failed!
currentDomain.add_AssemblyResolve(ResolveEventHandler myResolveEventHandler)
instantiateMyDynamicType currentDomain // OK!
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
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
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
注解
仅当evidence
同时提供了 ,或者保存动态程序集并将其重新加载到内存中时,才使用为 requiredPermissions
、 optionalPermissions
和 refusedPermissions
指定的权限请求。
注意
在开发发出动态程序集的代码期间,建议将 包含在 SecurityPermissionFlag.SkipVerification 中 refusedPermissions
。 在 SkipVerification 参数中包含 refusedPermissions
可确保验证 MSIL。 此方法的一个限制是,当与需要完全信任的代码一起使用时,它也会导致 SecurityException 引发。
定义动态 Assembly时,只有完全信任的调用方才能提供其 evidence
。 运行时将通过安全策略映射 Evidence ,以确定授予的权限。 部分受信任的调用方必须提供 null evidence
。 如果 evidence
为 null
,则运行时会将权限集(即当前授予和拒绝集)从调用者的 Assembly 复制到正在定义的动态 Assembly ,并将策略标记为已解决。
如果将动态 Assembly 保存到磁盘,则后续加载将根据与保存 的位置 Assembly 关联的策略获得授权。
此方法应仅用于定义当前应用程序域中的动态程序集。 有关详细信息,请参阅 Load(AssemblyName) 方法重载。
适用于
DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, PermissionSet, PermissionSet, PermissionSet)
注意
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:
virtual System::Reflection::Emit::AssemblyBuilder ^ DefineDynamicAssembly(System::Reflection::AssemblyName ^ name, System::Reflection::Emit::AssemblyBuilderAccess access, System::String ^ dir, System::Security::PermissionSet ^ requiredPermissions, System::Security::PermissionSet ^ optionalPermissions, System::Security::PermissionSet ^ refusedPermissions);
public System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, string dir, System.Security.PermissionSet requiredPermissions, System.Security.PermissionSet optionalPermissions, System.Security.PermissionSet refusedPermissions);
[System.Obsolete("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 System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, string dir, System.Security.PermissionSet requiredPermissions, System.Security.PermissionSet optionalPermissions, System.Security.PermissionSet refusedPermissions);
abstract member DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * string * System.Security.PermissionSet * System.Security.PermissionSet * System.Security.PermissionSet -> System.Reflection.Emit.AssemblyBuilder
override this.DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * string * System.Security.PermissionSet * System.Security.PermissionSet * System.Security.PermissionSet -> System.Reflection.Emit.AssemblyBuilder
[<System.Obsolete("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.")>]
abstract member DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * string * System.Security.PermissionSet * System.Security.PermissionSet * System.Security.PermissionSet -> System.Reflection.Emit.AssemblyBuilder
override this.DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * string * System.Security.PermissionSet * System.Security.PermissionSet * System.Security.PermissionSet -> System.Reflection.Emit.AssemblyBuilder
Public Function DefineDynamicAssembly (name As AssemblyName, access As AssemblyBuilderAccess, dir As String, requiredPermissions As PermissionSet, optionalPermissions As PermissionSet, refusedPermissions As PermissionSet) As AssemblyBuilder
参数
- name
- AssemblyName
动态程序集的唯一标识。
- access
- AssemblyBuilderAccess
访问动态程序集所采用的模式。
- dir
- String
保存程序集的目录的名称。 如果 dir
是 null
,目录将默认为当前目录。
- requiredPermissions
- PermissionSet
必需的权限请求。
- optionalPermissions
- PermissionSet
可选的权限请求。
- refusedPermissions
- PermissionSet
被拒绝的权限请求。
返回
具有指定名称和功能的动态程序集。
实现
- 属性
例外
name
为 null
。
在卸载的应用程序域上尝试该操作。
示例
下面的示例演示 DefineDynamicAssembly 了 方法和 AssemblyResolve 事件。
若要运行此代码示例,必须提供完全限定的程序集名称。 有关如何获取完全限定程序集名称的信息,请参阅 程序集名称。
using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
ref class Test
{
public:
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( dynamic_cast<AppDomain^>(sender) );
}
static Assembly^ DefineDynamicAssembly( AppDomain^ domain )
{
// Build a dynamic assembly using Reflection Emit API.
AssemblyName^ assemblyName = gcnew 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, nullptr );
ILGenerator^ ilGenerator = constructorBuilder->GetILGenerator();
ilGenerator->EmitWriteLine( "MyDynamicType instantiated!" );
ilGenerator->Emit( OpCodes::Ret );
typeBuilder->CreateType();
return assemblyBuilder;
}
};
int main()
{
AppDomain^ currentDomain = AppDomain::CurrentDomain;
Test::InstantiateMyDynamicType( currentDomain ); // Failed!
currentDomain->AssemblyResolve += gcnew ResolveEventHandler( Test::MyResolveEventHandler );
Test::InstantiateMyDynamicType( currentDomain ); // OK!
}
using System;
using System.Reflection;
using System.Reflection.Emit;
class DefinedDynamicAssemblySnippet {
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;
}
}
open System
open System.Reflection
open System.Reflection.Emit
let instantiateMyDynamicType (domain: AppDomain) =
try
// You must supply a valid fully qualified assembly name here.
domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType")
|> ignore
with e ->
printfn $"{e.Message}"
let defineDynamicAssembly (domain: AppDomain) =
// Build a dynamic assembly using Reflection Emit API.
let assemblyName = AssemblyName()
assemblyName.Name <- "MyDynamicAssembly"
let assemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run)
let moduleBuilder = assemblyBuilder.DefineDynamicModule "MyDynamicModule"
let typeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public)
let constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, null)
let ilGenerator = constructorBuilder.GetILGenerator()
ilGenerator.EmitWriteLine "MyDynamicType instantiated!"
ilGenerator.Emit OpCodes.Ret
typeBuilder.CreateType() |> ignore
assemblyBuilder
let myResolveEventHandler (sender: obj) _ =
defineDynamicAssembly (sender :?> AppDomain)
:> Assembly
let currentDomain = AppDomain.CurrentDomain
instantiateMyDynamicType currentDomain // Failed!
currentDomain.add_AssemblyResolve(ResolveEventHandler myResolveEventHandler)
instantiateMyDynamicType currentDomain // OK!
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
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
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
注解
除非动态程序集已保存并重新加载到内存中,否则不会使用为 requiredPermissions
、 optionalPermissions
和 refusedPermissions
指定的权限请求。 若要为永远不会保存到磁盘的暂时性程序集指定权限请求,请使用 方法的 DefineDynamicAssembly 重载,该方法指定证据和请求的权限,并提供 对象 Evidence 。
注意
在开发发出动态程序集的代码期间,建议使用 方法的 DefineDynamicAssembly 重载来指定证据和权限,提供希望动态程序集具有的证据,并包含在 SecurityPermissionFlag.SkipVerification 中 refusedPermissions
。 在 SkipVerification 参数中包含 refusedPermissions
可确保验证 MSIL。 此方法的一个限制是,当与需要完全信任的代码一起使用时,它也会导致 SecurityException 引发。
此方法应仅用于定义当前应用程序域中的动态程序集。 有关详细信息,请参阅 Load(AssemblyName) 方法重载。
适用于
DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, Evidence, PermissionSet, PermissionSet, PermissionSet)
注意
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:
virtual System::Reflection::Emit::AssemblyBuilder ^ DefineDynamicAssembly(System::Reflection::AssemblyName ^ name, System::Reflection::Emit::AssemblyBuilderAccess access, System::Security::Policy::Evidence ^ evidence, System::Security::PermissionSet ^ requiredPermissions, System::Security::PermissionSet ^ optionalPermissions, System::Security::PermissionSet ^ refusedPermissions);
public System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, System.Security.Policy.Evidence evidence, System.Security.PermissionSet requiredPermissions, System.Security.PermissionSet optionalPermissions, System.Security.PermissionSet refusedPermissions);
[System.Obsolete("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 System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, System.Security.Policy.Evidence evidence, System.Security.PermissionSet requiredPermissions, System.Security.PermissionSet optionalPermissions, System.Security.PermissionSet refusedPermissions);
abstract member DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * System.Security.Policy.Evidence * System.Security.PermissionSet * System.Security.PermissionSet * System.Security.PermissionSet -> System.Reflection.Emit.AssemblyBuilder
override this.DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * System.Security.Policy.Evidence * System.Security.PermissionSet * System.Security.PermissionSet * System.Security.PermissionSet -> System.Reflection.Emit.AssemblyBuilder
[<System.Obsolete("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.")>]
abstract member DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * System.Security.Policy.Evidence * System.Security.PermissionSet * System.Security.PermissionSet * System.Security.PermissionSet -> System.Reflection.Emit.AssemblyBuilder
override this.DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * System.Security.Policy.Evidence * System.Security.PermissionSet * System.Security.PermissionSet * System.Security.PermissionSet -> System.Reflection.Emit.AssemblyBuilder
Public Function DefineDynamicAssembly (name As AssemblyName, access As AssemblyBuilderAccess, evidence As Evidence, requiredPermissions As PermissionSet, optionalPermissions As PermissionSet, refusedPermissions As PermissionSet) As AssemblyBuilder
参数
- name
- AssemblyName
动态程序集的唯一标识。
- access
- AssemblyBuilderAccess
访问动态程序集所采用的模式。
- evidence
- Evidence
为动态程序集提供的证据。 该证据始终作为最后一组用于策略解析的证据来使用。
- requiredPermissions
- PermissionSet
必需的权限请求。
- optionalPermissions
- PermissionSet
可选的权限请求。
- refusedPermissions
- PermissionSet
被拒绝的权限请求。
返回
具有指定名称和功能的动态程序集。
实现
- 属性
例外
name
为 null
。
在卸载的应用程序域上尝试该操作。
示例
下面的示例演示 DefineDynamicAssembly 了 方法和 AssemblyResolve 事件。
若要运行此代码示例,必须提供完全限定的程序集名称。 有关如何获取完全限定程序集名称的信息,请参阅 程序集名称。
using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
ref class Test
{
public:
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( dynamic_cast<AppDomain^>(sender) );
}
static Assembly^ DefineDynamicAssembly( AppDomain^ domain )
{
// Build a dynamic assembly using Reflection Emit API.
AssemblyName^ assemblyName = gcnew 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, nullptr );
ILGenerator^ ilGenerator = constructorBuilder->GetILGenerator();
ilGenerator->EmitWriteLine( "MyDynamicType instantiated!" );
ilGenerator->Emit( OpCodes::Ret );
typeBuilder->CreateType();
return assemblyBuilder;
}
};
int main()
{
AppDomain^ currentDomain = AppDomain::CurrentDomain;
Test::InstantiateMyDynamicType( currentDomain ); // Failed!
currentDomain->AssemblyResolve += gcnew ResolveEventHandler( Test::MyResolveEventHandler );
Test::InstantiateMyDynamicType( currentDomain ); // OK!
}
using System;
using System.Reflection;
using System.Reflection.Emit;
class DefinedDynamicAssemblySnippet {
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;
}
}
open System
open System.Reflection
open System.Reflection.Emit
let instantiateMyDynamicType (domain: AppDomain) =
try
// You must supply a valid fully qualified assembly name here.
domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType")
|> ignore
with e ->
printfn $"{e.Message}"
let defineDynamicAssembly (domain: AppDomain) =
// Build a dynamic assembly using Reflection Emit API.
let assemblyName = AssemblyName()
assemblyName.Name <- "MyDynamicAssembly"
let assemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run)
let moduleBuilder = assemblyBuilder.DefineDynamicModule "MyDynamicModule"
let typeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public)
let constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, null)
let ilGenerator = constructorBuilder.GetILGenerator()
ilGenerator.EmitWriteLine "MyDynamicType instantiated!"
ilGenerator.Emit OpCodes.Ret
typeBuilder.CreateType() |> ignore
assemblyBuilder
let myResolveEventHandler (sender: obj) _ =
defineDynamicAssembly (sender :?> AppDomain)
:> Assembly
let currentDomain = AppDomain.CurrentDomain
instantiateMyDynamicType currentDomain // Failed!
currentDomain.add_AssemblyResolve(ResolveEventHandler myResolveEventHandler)
instantiateMyDynamicType currentDomain // OK!
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
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
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
注解
仅当evidence
同时提供了 ,或者保存动态程序集并将其重新加载到内存中时,才使用为 requiredPermissions
、 optionalPermissions
和 refusedPermissions
指定的权限请求。
注意
在开发发出动态程序集的代码期间,建议将 包含在 SecurityPermissionFlag.SkipVerification 中 refusedPermissions
。 在 SkipVerification 参数中包含 refusedPermissions
可确保验证 MSIL。 此方法的一个限制是,当与需要完全信任的代码一起使用时,它也会导致 SecurityException 引发。
定义动态 Assembly时,只有完全信任的调用方才能提供其 evidence
。 运行时将通过安全策略映射 Evidence ,以确定授予的权限。 部分受信任的调用方必须提供 null evidence
。 如果 evidence
为 null
,则运行时会将权限集(即当前授予和拒绝集)从调用者的 Assembly 复制到正在定义的动态 Assembly ,并将策略标记为已解决。
如果将动态 Assembly 保存到磁盘,则后续加载将根据与保存 的位置 Assembly 关联的策略获得授权。
此方法应仅用于定义当前应用程序域中的动态程序集。 有关详细信息,请参阅 Load(AssemblyName) 方法重载。
适用于
DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, Boolean, IEnumerable<CustomAttributeBuilder>)
使用指定名称、访问模式、存储目录和同步选项定义动态程序集。
public:
System::Reflection::Emit::AssemblyBuilder ^ DefineDynamicAssembly(System::Reflection::AssemblyName ^ name, System::Reflection::Emit::AssemblyBuilderAccess access, System::String ^ dir, bool isSynchronized, System::Collections::Generic::IEnumerable<System::Reflection::Emit::CustomAttributeBuilder ^> ^ assemblyAttributes);
public System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, string dir, bool isSynchronized, System.Collections.Generic.IEnumerable<System.Reflection.Emit.CustomAttributeBuilder> assemblyAttributes);
member this.DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * string * bool * seq<System.Reflection.Emit.CustomAttributeBuilder> -> System.Reflection.Emit.AssemblyBuilder
Public Function DefineDynamicAssembly (name As AssemblyName, access As AssemblyBuilderAccess, dir As String, isSynchronized As Boolean, assemblyAttributes As IEnumerable(Of CustomAttributeBuilder)) As AssemblyBuilder
参数
- name
- AssemblyName
动态程序集的唯一标识。
- access
- AssemblyBuilderAccess
访问动态程序集所采用的模式。
- dir
- String
保存动态程序集的目录的名称。 如果 dir
为 null
,则使用当前目录。
- isSynchronized
- Boolean
若要在动态程序集中同步模块、类型和成员的创建,则为 true
;否则为 false
。
- assemblyAttributes
- IEnumerable<CustomAttributeBuilder>
要应用于程序集的可枚举特性列表;如果无特性,则为 null
。
返回
具有指定名称和功能的动态程序集。
例外
name
为 null
。
在卸载的应用程序域上尝试该操作。
注解
使用此方法重载指定无法正常工作的属性,除非在创建动态程序集时应用这些属性。 例如,如果在创建动态程序集后添加安全属性(如 SecurityTransparentAttribute 和 SecurityCriticalAttribute ),则它们无法正常工作。
如果 isSynchronized
为 true
,则生成的 AssemblyBuilder 的以下方法将同步:DefineDynamicModule、、DefineResource、AddResourceFileGetDynamicModule、 SetEntryPoint和 Save。 如果在不同的线程上调用其中两个方法,则一个方法将阻塞,直到另一个方法完成。
另请参阅
适用于
DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, PermissionSet, PermissionSet, PermissionSet)
注意
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:
virtual System::Reflection::Emit::AssemblyBuilder ^ DefineDynamicAssembly(System::Reflection::AssemblyName ^ name, System::Reflection::Emit::AssemblyBuilderAccess access, System::Security::PermissionSet ^ requiredPermissions, System::Security::PermissionSet ^ optionalPermissions, System::Security::PermissionSet ^ refusedPermissions);
public System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, System.Security.PermissionSet requiredPermissions, System.Security.PermissionSet optionalPermissions, System.Security.PermissionSet refusedPermissions);
[System.Obsolete("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 System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, System.Security.PermissionSet requiredPermissions, System.Security.PermissionSet optionalPermissions, System.Security.PermissionSet refusedPermissions);
abstract member DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * System.Security.PermissionSet * System.Security.PermissionSet * System.Security.PermissionSet -> System.Reflection.Emit.AssemblyBuilder
override this.DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * System.Security.PermissionSet * System.Security.PermissionSet * System.Security.PermissionSet -> System.Reflection.Emit.AssemblyBuilder
[<System.Obsolete("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.")>]
abstract member DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * System.Security.PermissionSet * System.Security.PermissionSet * System.Security.PermissionSet -> System.Reflection.Emit.AssemblyBuilder
override this.DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * System.Security.PermissionSet * System.Security.PermissionSet * System.Security.PermissionSet -> System.Reflection.Emit.AssemblyBuilder
Public Function DefineDynamicAssembly (name As AssemblyName, access As AssemblyBuilderAccess, requiredPermissions As PermissionSet, optionalPermissions As PermissionSet, refusedPermissions As PermissionSet) As AssemblyBuilder
参数
- name
- AssemblyName
动态程序集的唯一标识。
- access
- AssemblyBuilderAccess
访问动态程序集所采用的模式。
- requiredPermissions
- PermissionSet
必需的权限请求。
- optionalPermissions
- PermissionSet
可选的权限请求。
- refusedPermissions
- PermissionSet
被拒绝的权限请求。
返回
具有指定名称和功能的动态程序集。
实现
- 属性
例外
name
为 null
。
在卸载的应用程序域上尝试该操作。
示例
下面的示例演示 DefineDynamicAssembly 了 方法和 AssemblyResolve 事件。
若要运行此代码示例,必须提供完全限定的程序集名称。 有关如何获取完全限定程序集名称的信息,请参阅 程序集名称。
using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
ref class Test
{
public:
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( dynamic_cast<AppDomain^>(sender) );
}
static Assembly^ DefineDynamicAssembly( AppDomain^ domain )
{
// Build a dynamic assembly using Reflection Emit API.
AssemblyName^ assemblyName = gcnew 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, nullptr );
ILGenerator^ ilGenerator = constructorBuilder->GetILGenerator();
ilGenerator->EmitWriteLine( "MyDynamicType instantiated!" );
ilGenerator->Emit( OpCodes::Ret );
typeBuilder->CreateType();
return assemblyBuilder;
}
};
int main()
{
AppDomain^ currentDomain = AppDomain::CurrentDomain;
Test::InstantiateMyDynamicType( currentDomain ); // Failed!
currentDomain->AssemblyResolve += gcnew ResolveEventHandler( Test::MyResolveEventHandler );
Test::InstantiateMyDynamicType( currentDomain ); // OK!
}
using System;
using System.Reflection;
using System.Reflection.Emit;
class DefinedDynamicAssemblySnippet {
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;
}
}
open System
open System.Reflection
open System.Reflection.Emit
let instantiateMyDynamicType (domain: AppDomain) =
try
// You must supply a valid fully qualified assembly name here.
domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType")
|> ignore
with e ->
printfn $"{e.Message}"
let defineDynamicAssembly (domain: AppDomain) =
// Build a dynamic assembly using Reflection Emit API.
let assemblyName = AssemblyName()
assemblyName.Name <- "MyDynamicAssembly"
let assemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run)
let moduleBuilder = assemblyBuilder.DefineDynamicModule "MyDynamicModule"
let typeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public)
let constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, null)
let ilGenerator = constructorBuilder.GetILGenerator()
ilGenerator.EmitWriteLine "MyDynamicType instantiated!"
ilGenerator.Emit OpCodes.Ret
typeBuilder.CreateType() |> ignore
assemblyBuilder
let myResolveEventHandler (sender: obj) _ =
defineDynamicAssembly (sender :?> AppDomain)
:> Assembly
let currentDomain = AppDomain.CurrentDomain
instantiateMyDynamicType currentDomain // Failed!
currentDomain.add_AssemblyResolve(ResolveEventHandler myResolveEventHandler)
instantiateMyDynamicType currentDomain // OK!
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
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
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
注解
除非动态程序集已保存并重新加载到内存中,否则不会使用为 requiredPermissions
、 optionalPermissions
和 refusedPermissions
指定的权限请求。 若要为永远不会保存到磁盘的暂时性程序集指定权限请求,请使用 方法的 DefineDynamicAssembly 重载,该方法指定证据和请求的权限,并提供 对象 Evidence 。
注意
在开发发出动态程序集的代码期间,建议使用 方法的 DefineDynamicAssembly 重载来指定证据和权限,提供希望动态程序集具有的证据,并包含在 SecurityPermissionFlag.SkipVerification 中 refusedPermissions
。 在 SkipVerification 参数中包含 refusedPermissions
可确保验证 MSIL。 此方法的一个限制是,当与需要完全信任的代码一起使用时,它也会导致 SecurityException 引发。
此方法应仅用于定义当前应用程序域中的动态程序集。 有关详细信息,请参阅 Load(AssemblyName) 方法重载 。
适用于
DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, Evidence)
注意
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:
virtual System::Reflection::Emit::AssemblyBuilder ^ DefineDynamicAssembly(System::Reflection::AssemblyName ^ name, System::Reflection::Emit::AssemblyBuilderAccess access, System::String ^ dir, System::Security::Policy::Evidence ^ evidence);
public System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, string dir, System.Security.Policy.Evidence evidence);
[System.Obsolete("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 System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, string dir, System.Security.Policy.Evidence evidence);
abstract member DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * string * System.Security.Policy.Evidence -> System.Reflection.Emit.AssemblyBuilder
override this.DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * string * System.Security.Policy.Evidence -> System.Reflection.Emit.AssemblyBuilder
[<System.Obsolete("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.")>]
abstract member DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * string * System.Security.Policy.Evidence -> System.Reflection.Emit.AssemblyBuilder
override this.DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * string * System.Security.Policy.Evidence -> System.Reflection.Emit.AssemblyBuilder
Public Function DefineDynamicAssembly (name As AssemblyName, access As AssemblyBuilderAccess, dir As String, evidence As Evidence) As AssemblyBuilder
参数
- name
- AssemblyName
动态程序集的唯一标识。
- access
- AssemblyBuilderAccess
访问动态程序集所采用的模式。
- dir
- String
保存程序集的目录的名称。 如果 dir
是 null
,目录将默认为当前目录。
- evidence
- Evidence
为动态程序集提供的证据。 该证据始终作为最后一组用于策略解析的证据来使用。
返回
具有指定名称和功能的动态程序集。
实现
- 属性
例外
name
为 null
。
在卸载的应用程序域上尝试该操作。
示例
下面的示例演示 DefineDynamicAssembly 了 方法和 AssemblyResolve 事件。
若要运行此代码示例,必须提供完全限定的程序集名称。 有关如何获取完全限定程序集名称的信息,请参阅 程序集名称。
using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
ref class Test
{
public:
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( dynamic_cast<AppDomain^>(sender) );
}
static Assembly^ DefineDynamicAssembly( AppDomain^ domain )
{
// Build a dynamic assembly using Reflection Emit API.
AssemblyName^ assemblyName = gcnew 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, nullptr );
ILGenerator^ ilGenerator = constructorBuilder->GetILGenerator();
ilGenerator->EmitWriteLine( "MyDynamicType instantiated!" );
ilGenerator->Emit( OpCodes::Ret );
typeBuilder->CreateType();
return assemblyBuilder;
}
};
int main()
{
AppDomain^ currentDomain = AppDomain::CurrentDomain;
Test::InstantiateMyDynamicType( currentDomain ); // Failed!
currentDomain->AssemblyResolve += gcnew ResolveEventHandler( Test::MyResolveEventHandler );
Test::InstantiateMyDynamicType( currentDomain ); // OK!
}
using System;
using System.Reflection;
using System.Reflection.Emit;
class DefinedDynamicAssemblySnippet {
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;
}
}
open System
open System.Reflection
open System.Reflection.Emit
let instantiateMyDynamicType (domain: AppDomain) =
try
// You must supply a valid fully qualified assembly name here.
domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType")
|> ignore
with e ->
printfn $"{e.Message}"
let defineDynamicAssembly (domain: AppDomain) =
// Build a dynamic assembly using Reflection Emit API.
let assemblyName = AssemblyName()
assemblyName.Name <- "MyDynamicAssembly"
let assemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run)
let moduleBuilder = assemblyBuilder.DefineDynamicModule "MyDynamicModule"
let typeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public)
let constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, null)
let ilGenerator = constructorBuilder.GetILGenerator()
ilGenerator.EmitWriteLine "MyDynamicType instantiated!"
ilGenerator.Emit OpCodes.Ret
typeBuilder.CreateType() |> ignore
assemblyBuilder
let myResolveEventHandler (sender: obj) _ =
defineDynamicAssembly (sender :?> AppDomain)
:> Assembly
let currentDomain = AppDomain.CurrentDomain
instantiateMyDynamicType currentDomain // Failed!
currentDomain.add_AssemblyResolve(ResolveEventHandler myResolveEventHandler)
instantiateMyDynamicType currentDomain // OK!
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
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
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
注解
定义动态 Assembly时,只有完全信任的调用方才能提供其 evidence
。 运行时将通过安全策略映射 Evidence ,以确定授予的权限。 部分受信任的调用方必须提供 null evidence
。 如果 evidence
为 null
,则运行时会将权限集(即当前授予和拒绝集)从调用者的 Assembly 复制到正在定义的动态 Assembly ,并将策略标记为已解决。
如果将动态 Assembly 保存到磁盘,则后续加载将根据与保存 的位置 Assembly 关联的策略获得授权。
此方法应仅用于定义当前应用程序域中的动态程序集。 有关详细信息,请参阅 Load(AssemblyName) 方法重载。
注意
在开发发出动态程序集的代码期间,建议使用 方法的 DefineDynamicAssembly 重载来指定证据和权限,提供希望动态程序集具有的证据,并包含在 SecurityPermissionFlag.SkipVerification 中 refusedPermissions
。 在 SkipVerification 参数中包含 refusedPermissions
可确保验证 MSIL。 此方法的一个限制是,当与需要完全信任的代码一起使用时,它也会导致 SecurityException 引发。
适用于
DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, IEnumerable<CustomAttributeBuilder>, SecurityContextSource)
定义具有指定名称、访问模式和自定义特性的动态程序集,并将指定源用于动态程序集的安全上下文。
public:
System::Reflection::Emit::AssemblyBuilder ^ DefineDynamicAssembly(System::Reflection::AssemblyName ^ name, System::Reflection::Emit::AssemblyBuilderAccess access, System::Collections::Generic::IEnumerable<System::Reflection::Emit::CustomAttributeBuilder ^> ^ assemblyAttributes, System::Security::SecurityContextSource securityContextSource);
public System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, System.Collections.Generic.IEnumerable<System.Reflection.Emit.CustomAttributeBuilder> assemblyAttributes, System.Security.SecurityContextSource securityContextSource);
member this.DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * seq<System.Reflection.Emit.CustomAttributeBuilder> * System.Security.SecurityContextSource -> System.Reflection.Emit.AssemblyBuilder
Public Function DefineDynamicAssembly (name As AssemblyName, access As AssemblyBuilderAccess, assemblyAttributes As IEnumerable(Of CustomAttributeBuilder), securityContextSource As SecurityContextSource) As AssemblyBuilder
参数
- name
- AssemblyName
动态程序集的唯一标识。
- access
- AssemblyBuilderAccess
动态程序集的访问模式。
- assemblyAttributes
- IEnumerable<CustomAttributeBuilder>
要应用于程序集的可枚举特性列表;如果无特性,则为 null
。
- securityContextSource
- SecurityContextSource
安全上下文的源。
返回
具有指定名称和功能的动态程序集。
例外
name
为 null
。
在卸载的应用程序域上尝试该操作。
securityContextSource
的值不是枚举值之一。
注解
使用此方法重载指定无法正常工作的属性,除非在创建动态程序集时应用这些属性。 例如,如果在创建动态程序集后添加安全属性(如 SecurityTransparentAttribute 和 SecurityCriticalAttribute ),则它们无法正常工作。
此方法应仅用于定义当前应用程序域中的动态程序集。 有关此限制的详细信息,请参阅 Load(AssemblyName) 方法重载。
适用于
DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String)
使用指定名称、访问模式和存储目录定义动态程序集。
public:
virtual System::Reflection::Emit::AssemblyBuilder ^ DefineDynamicAssembly(System::Reflection::AssemblyName ^ name, System::Reflection::Emit::AssemblyBuilderAccess access, System::String ^ dir);
public System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, string dir);
abstract member DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * string -> System.Reflection.Emit.AssemblyBuilder
override this.DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * string -> System.Reflection.Emit.AssemblyBuilder
Public Function DefineDynamicAssembly (name As AssemblyName, access As AssemblyBuilderAccess, dir As String) As AssemblyBuilder
参数
- name
- AssemblyName
动态程序集的唯一标识。
- access
- AssemblyBuilderAccess
访问动态程序集所采用的模式。
- dir
- String
保存程序集的目录的名称。 如果 dir
是 null
,目录将默认为当前目录。
返回
具有指定名称和功能的动态程序集。
实现
例外
name
为 null
。
在卸载的应用程序域上尝试该操作。
示例
下面的示例演示 DefineDynamicAssembly 了 方法和 AssemblyResolve 事件。
若要运行此代码示例,必须提供完全限定的程序集名称。 有关如何获取完全限定程序集名称的信息,请参阅 程序集名称。
using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
ref class Test
{
public:
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( dynamic_cast<AppDomain^>(sender) );
}
static Assembly^ DefineDynamicAssembly( AppDomain^ domain )
{
// Build a dynamic assembly using Reflection Emit API.
AssemblyName^ assemblyName = gcnew 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, nullptr );
ILGenerator^ ilGenerator = constructorBuilder->GetILGenerator();
ilGenerator->EmitWriteLine( "MyDynamicType instantiated!" );
ilGenerator->Emit( OpCodes::Ret );
typeBuilder->CreateType();
return assemblyBuilder;
}
};
int main()
{
AppDomain^ currentDomain = AppDomain::CurrentDomain;
Test::InstantiateMyDynamicType( currentDomain ); // Failed!
currentDomain->AssemblyResolve += gcnew ResolveEventHandler( Test::MyResolveEventHandler );
Test::InstantiateMyDynamicType( currentDomain ); // OK!
}
using System;
using System.Reflection;
using System.Reflection.Emit;
class DefinedDynamicAssemblySnippet {
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;
}
}
open System
open System.Reflection
open System.Reflection.Emit
let instantiateMyDynamicType (domain: AppDomain) =
try
// You must supply a valid fully qualified assembly name here.
domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType")
|> ignore
with e ->
printfn $"{e.Message}"
let defineDynamicAssembly (domain: AppDomain) =
// Build a dynamic assembly using Reflection Emit API.
let assemblyName = AssemblyName()
assemblyName.Name <- "MyDynamicAssembly"
let assemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run)
let moduleBuilder = assemblyBuilder.DefineDynamicModule "MyDynamicModule"
let typeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public)
let constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, null)
let ilGenerator = constructorBuilder.GetILGenerator()
ilGenerator.EmitWriteLine "MyDynamicType instantiated!"
ilGenerator.Emit OpCodes.Ret
typeBuilder.CreateType() |> ignore
assemblyBuilder
let myResolveEventHandler (sender: obj) _ =
defineDynamicAssembly (sender :?> AppDomain)
:> Assembly
let currentDomain = AppDomain.CurrentDomain
instantiateMyDynamicType currentDomain // Failed!
currentDomain.add_AssemblyResolve(ResolveEventHandler myResolveEventHandler)
instantiateMyDynamicType currentDomain // OK!
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
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
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
注解
此方法应仅用于定义当前应用程序域中的动态程序集。 有关详细信息,请参阅 Load(AssemblyName) 方法重载。
注意
在开发发出动态程序集的代码期间,建议使用 方法的 DefineDynamicAssembly 重载来指定证据和权限,提供希望动态程序集具有的证据,并包含在 SecurityPermissionFlag.SkipVerification 中 refusedPermissions
。 在 SkipVerification 参数中包含 refusedPermissions
可确保验证 MSIL。 此方法的一个限制是,当与需要完全信任的代码一起使用时,它也会导致 SecurityException 引发。
适用于
DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, Evidence)
注意
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:
virtual System::Reflection::Emit::AssemblyBuilder ^ DefineDynamicAssembly(System::Reflection::AssemblyName ^ name, System::Reflection::Emit::AssemblyBuilderAccess access, System::Security::Policy::Evidence ^ evidence);
public System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, System.Security.Policy.Evidence evidence);
[System.Obsolete("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 System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, System.Security.Policy.Evidence evidence);
abstract member DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * System.Security.Policy.Evidence -> System.Reflection.Emit.AssemblyBuilder
override this.DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * System.Security.Policy.Evidence -> System.Reflection.Emit.AssemblyBuilder
[<System.Obsolete("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.")>]
abstract member DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * System.Security.Policy.Evidence -> System.Reflection.Emit.AssemblyBuilder
override this.DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * System.Security.Policy.Evidence -> System.Reflection.Emit.AssemblyBuilder
Public Function DefineDynamicAssembly (name As AssemblyName, access As AssemblyBuilderAccess, evidence As Evidence) As AssemblyBuilder
参数
- name
- AssemblyName
动态程序集的唯一标识。
- access
- AssemblyBuilderAccess
访问动态程序集所采用的模式。
- evidence
- Evidence
为动态程序集提供的证据。 该证据始终作为最后一组用于策略解析的证据来使用。
返回
具有指定名称和功能的动态程序集。
实现
- 属性
例外
name
为 null
。
在卸载的应用程序域上尝试该操作。
示例
下面的示例演示 DefineDynamicAssembly 了 方法和 AssemblyResolve 事件。
首先,代码示例尝试通过调用CreateInstance具有无效程序集名称的方法来创建 的MyDynamicType
实例,并捕获生成的异常。
然后,代码示例为 AssemblyResolve 事件添加事件处理程序,并再次尝试创建 的MyDynamicType
实例。 在调用 CreateInstance期间, AssemblyResolve 会为无效程序集引发 事件。 事件处理程序创建一个动态程序集,该程序集包含名为 的类型 MyDynamicType
,为该类型提供无参数构造函数,并返回新的动态程序集。 然后,对 的 CreateInstance 调用成功完成,并且 的 MyDynamicType
构造函数在控制台上显示一条消息。
using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
ref class Test
{
public:
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( dynamic_cast<AppDomain^>(sender) );
}
static Assembly^ DefineDynamicAssembly( AppDomain^ domain )
{
// Build a dynamic assembly using Reflection Emit API.
AssemblyName^ assemblyName = gcnew 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, nullptr );
ILGenerator^ ilGenerator = constructorBuilder->GetILGenerator();
ilGenerator->EmitWriteLine( "MyDynamicType instantiated!" );
ilGenerator->Emit( OpCodes::Ret );
typeBuilder->CreateType();
return assemblyBuilder;
}
};
int main()
{
AppDomain^ currentDomain = AppDomain::CurrentDomain;
Test::InstantiateMyDynamicType( currentDomain ); // Failed!
currentDomain->AssemblyResolve += gcnew ResolveEventHandler( Test::MyResolveEventHandler );
Test::InstantiateMyDynamicType( currentDomain ); // OK!
}
using System;
using System.Reflection;
using System.Reflection.Emit;
class DefinedDynamicAssemblySnippet {
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;
}
}
open System
open System.Reflection
open System.Reflection.Emit
let instantiateMyDynamicType (domain: AppDomain) =
try
// You must supply a valid fully qualified assembly name here.
domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType")
|> ignore
with e ->
printfn $"{e.Message}"
let defineDynamicAssembly (domain: AppDomain) =
// Build a dynamic assembly using Reflection Emit API.
let assemblyName = AssemblyName()
assemblyName.Name <- "MyDynamicAssembly"
let assemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run)
let moduleBuilder = assemblyBuilder.DefineDynamicModule "MyDynamicModule"
let typeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public)
let constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, null)
let ilGenerator = constructorBuilder.GetILGenerator()
ilGenerator.EmitWriteLine "MyDynamicType instantiated!"
ilGenerator.Emit OpCodes.Ret
typeBuilder.CreateType() |> ignore
assemblyBuilder
let myResolveEventHandler (sender: obj) _ =
defineDynamicAssembly (sender :?> AppDomain)
:> Assembly
let currentDomain = AppDomain.CurrentDomain
instantiateMyDynamicType currentDomain // Failed!
currentDomain.add_AssemblyResolve(ResolveEventHandler myResolveEventHandler)
instantiateMyDynamicType currentDomain // OK!
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
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
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
注解
定义动态 时,只有完全受信任的调用方才能提供其 evidence
。Assembly 运行时将通过安全策略映射 Evidence ,以确定授予的权限。 部分受信任的调用方必须提供 null evidence
。 如果 evidence
为 null
,则运行时将权限集(即当前授予和拒绝集)从调用方复制到 Assembly 要定义的动态 Assembly ,并将策略标记为已解决。
如果将动态 Assembly 保存到磁盘,则后续加载将根据与保存 位置 Assembly 关联的策略获得授权。
此方法仅用于定义当前应用程序域中的动态程序集。 有关详细信息,请参阅 Load(AssemblyName) 方法重载。
注意
在开发发出动态程序集的代码期间,建议使用 方法的 DefineDynamicAssembly 重载来指定证据和权限,提供希望动态程序集具有的证据,并包含在 SecurityPermissionFlag.SkipVerification 中 refusedPermissions
。 在 SkipVerification 参数中包含 refusedPermissions
可确保验证 MSIL。 此方法的一个限制是,当与需要完全信任的代码一起使用时,它也会导致 SecurityException 引发。
适用于
DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, IEnumerable<CustomAttributeBuilder>)
使用指定的名称、访问模式和自定义特性定义动态程序集。
public:
System::Reflection::Emit::AssemblyBuilder ^ DefineDynamicAssembly(System::Reflection::AssemblyName ^ name, System::Reflection::Emit::AssemblyBuilderAccess access, System::Collections::Generic::IEnumerable<System::Reflection::Emit::CustomAttributeBuilder ^> ^ assemblyAttributes);
public System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, System.Collections.Generic.IEnumerable<System.Reflection.Emit.CustomAttributeBuilder> assemblyAttributes);
member this.DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * seq<System.Reflection.Emit.CustomAttributeBuilder> -> System.Reflection.Emit.AssemblyBuilder
Public Function DefineDynamicAssembly (name As AssemblyName, access As AssemblyBuilderAccess, assemblyAttributes As IEnumerable(Of CustomAttributeBuilder)) As AssemblyBuilder
参数
- name
- AssemblyName
动态程序集的唯一标识。
- access
- AssemblyBuilderAccess
动态程序集的访问模式。
- assemblyAttributes
- IEnumerable<CustomAttributeBuilder>
要应用于程序集的可枚举特性列表;如果无特性,则为 null
。
返回
具有指定名称和功能的动态程序集。
例外
name
为 null
。
在卸载的应用程序域上尝试该操作。
示例
以下代码示例演示如何创建具有 的 SecurityTransparentAttribute动态程序集。 必须将 特性指定为 对象数组的 CustomAttributeBuilder 元素。
创建 CustomAttributeBuilder 的第一步是获取 特性的构造函数。 构造函数没有参数,因此 GetConstructor 使用对象的空数组 Type 调用 方法,以表示参数的类型。 第二步是将生成的 ConstructorInfo 对象传递给 类的 CustomAttributeBuilder 构造函数,以及类型 Object 为的空数组来表示参数。
然后,生成的 CustomAttributeBuilder 作为数组的唯一元素传递给 DefineDynamicAssembly 方法。
示例代码在新的动态程序集中定义模块和类型,然后显示程序集的属性。
using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
using namespace System::Security;
void main()
{
// Create a CustomAttributeBuilder for the assembly attribute.
//
// SecurityTransparentAttribute has a parameterless constructor,
// which is retrieved by passing an array of empty types for the
// constructor's parameter types. The CustomAttributeBuilder is
// then created by passing the ConstructorInfo and an empty array
// of objects to represent the parameters.
//
ConstructorInfo^ transparentCtor =
SecurityTransparentAttribute::typeid->GetConstructor(
Type::EmptyTypes);
CustomAttributeBuilder^ transparent = gcnew CustomAttributeBuilder(
transparentCtor,
gcnew array<Object^> {} );
// Create a dynamic assembly using the attribute. The attribute is
// passed as an array with one element.
AssemblyName^ aName = gcnew AssemblyName("EmittedAssembly");
AssemblyBuilder^ ab = AppDomain::CurrentDomain->DefineDynamicAssembly(
aName,
AssemblyBuilderAccess::Run,
gcnew array<CustomAttributeBuilder^> { transparent } );
ModuleBuilder^ mb = ab->DefineDynamicModule( aName->Name );
TypeBuilder^ tb = mb->DefineType(
"MyDynamicType",
TypeAttributes::Public );
tb->CreateType();
Console::WriteLine("{0}\nAssembly attributes:", ab);
for each (Attribute^ attr in ab->GetCustomAttributes(true))
{
Console::WriteLine("\t{0}", attr);
}
};
/* This code example produces the following output:
EmittedAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
Assembly attributes:
System.Security.SecurityTransparentAttribute
*/
using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Security;
class CustomAttributeBuilderSnippet
{
static void Main()
{
// Create a CustomAttributeBuilder for the assembly attribute.
//
// SecurityTransparentAttribute has a parameterless constructor,
// which is retrieved by passing an array of empty types for the
// constructor's parameter types. The CustomAttributeBuilder is
// then created by passing the ConstructorInfo and an empty array
// of objects to represent the parameters.
//
ConstructorInfo transparentCtor =
typeof(SecurityTransparentAttribute).GetConstructor(
Type.EmptyTypes);
CustomAttributeBuilder transparent = new CustomAttributeBuilder(
transparentCtor,
new Object[] {} );
// Create a dynamic assembly using the attribute. The attribute is
// passed as an array with one element.
AssemblyName aName = new AssemblyName("EmittedAssembly");
AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly(
aName,
AssemblyBuilderAccess.Run,
new CustomAttributeBuilder[] { transparent } );
ModuleBuilder mb = ab.DefineDynamicModule( aName.Name );
TypeBuilder tb = mb.DefineType(
"MyDynamicType",
TypeAttributes.Public );
tb.CreateType();
Console.WriteLine("{0}\nAssembly attributes:", ab);
foreach (Attribute attr in ab.GetCustomAttributes(true))
{
Console.WriteLine("\t{0}", attr);
}
}
}
/* This code example produces the following output:
EmittedAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
Assembly attributes:
System.Security.SecurityTransparentAttribute
*/
open System
open System.Reflection
open System.Reflection.Emit
open System.Security
// Create a CustomAttributeBuilder for the assembly attribute.
//
// SecurityTransparentAttribute has a parameterless constructor,
// which is retrieved by passing an array of empty types for the
// constructor's parameter types. The CustomAttributeBuilder is
// then created by passing the ConstructorInfo and an empty array
// of objects to represent the parameters.
//
let transparentCtor =
typeof<SecurityTransparentAttribute>.GetConstructor(Type.EmptyTypes)
let transparent = CustomAttributeBuilder(transparentCtor, [||])
// Create a dynamic assembly using the attribute. The attribute is
// passed as an array with one element.
let aName = AssemblyName "EmittedAssembly"
let ab = AppDomain.CurrentDomain.DefineDynamicAssembly(
aName,
AssemblyBuilderAccess.Run,
[| transparent |])
let mb = ab.DefineDynamicModule aName.Name
let tb = mb.DefineType(
"MyDynamicType",
TypeAttributes.Public )
tb.CreateType() |> ignore
printfn $"{ab}\nAssembly attributes:"
for attr in ab.GetCustomAttributes true do
printfn $"\t{attr}"
(* This code example produces the following output:
EmittedAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
Assembly attributes:
System.Security.SecurityTransparentAttribute
*)
Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Security
Module Example
Sub Main()
' Create a CustomAttributeBuilder for the assembly attribute.
'
' SecurityTransparentAttribute has a parameterless constructor,
' which is retrieved by passing an array of empty types for the
' constructor's parameter types. The CustomAttributeBuilder is
' then created by passing the ConstructorInfo and an empty array
' of objects to represent the parameters.
'
Dim transparentCtor As ConstructorInfo = _
GetType(SecurityTransparentAttribute).GetConstructor( _
Type.EmptyTypes)
Dim transparent As New CustomAttributeBuilder( _
transparentCtor, _
New Object() {} )
' Create a dynamic assembly Imports the attribute. The attribute is
' passed as an array with one element.
Dim aName As New AssemblyName("EmittedAssembly")
Dim ab As AssemblyBuilder = _
AppDomain.CurrentDomain.DefineDynamicAssembly( _
aName, _
AssemblyBuilderAccess.Run, _
New CustomAttributeBuilder() { transparent } )
Dim mb As ModuleBuilder = ab.DefineDynamicModule( aName.Name )
Dim tb As TypeBuilder = mb.DefineType( _
"MyDynamicType", _
TypeAttributes.Public )
tb.CreateType()
Console.WriteLine("{0}" & vbLf & "Assembly attributes:", ab)
For Each attr As Attribute In ab.GetCustomAttributes(True)
Console.WriteLine(vbTab & "{0}", attr)
Next
End Sub
End Module
' This code example produces the following output:
'
'EmittedAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
'Assembly attributes:
' System.Security.SecurityTransparentAttribute
注解
使用此方法重载指定无法正常工作的属性,除非在创建动态程序集时应用这些属性。 例如,如果在创建动态程序集后添加和 等SecurityTransparentAttributeSecurityCriticalAttribute安全属性,则它们将无法正常工作。
此方法应仅用于定义当前应用程序域中的动态程序集。 有关此限制的详细信息,请参阅 Load(AssemblyName) 方法重载。
.NET Framework 3.5 中引入了此方法重载。
另请参阅
适用于
DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess)
以指定名称和访问模式定义动态程序集。
public:
virtual System::Reflection::Emit::AssemblyBuilder ^ DefineDynamicAssembly(System::Reflection::AssemblyName ^ name, System::Reflection::Emit::AssemblyBuilderAccess access);
public System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access);
abstract member DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess -> System.Reflection.Emit.AssemblyBuilder
override this.DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess -> System.Reflection.Emit.AssemblyBuilder
Public Function DefineDynamicAssembly (name As AssemblyName, access As AssemblyBuilderAccess) As AssemblyBuilder
参数
- name
- AssemblyName
动态程序集的唯一标识。
- access
- AssemblyBuilderAccess
动态程序集的访问模式。
返回
含指定名称和访问模式的动态程序集。
实现
例外
name
为 null
。
在卸载的应用程序域上尝试该操作。
示例
以下示例演示 DefineDynamicAssembly 了 方法和 AssemblyResolve 事件。
若要运行此代码示例,必须提供完全限定的程序集名称。 有关如何获取完全限定程序集名称的信息,请参阅 程序集名称。
using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
ref class Test
{
public:
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( dynamic_cast<AppDomain^>(sender) );
}
static Assembly^ DefineDynamicAssembly( AppDomain^ domain )
{
// Build a dynamic assembly using Reflection Emit API.
AssemblyName^ assemblyName = gcnew 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, nullptr );
ILGenerator^ ilGenerator = constructorBuilder->GetILGenerator();
ilGenerator->EmitWriteLine( "MyDynamicType instantiated!" );
ilGenerator->Emit( OpCodes::Ret );
typeBuilder->CreateType();
return assemblyBuilder;
}
};
int main()
{
AppDomain^ currentDomain = AppDomain::CurrentDomain;
Test::InstantiateMyDynamicType( currentDomain ); // Failed!
currentDomain->AssemblyResolve += gcnew ResolveEventHandler( Test::MyResolveEventHandler );
Test::InstantiateMyDynamicType( currentDomain ); // OK!
}
using System;
using System.Reflection;
using System.Reflection.Emit;
class DefinedDynamicAssemblySnippet {
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;
}
}
open System
open System.Reflection
open System.Reflection.Emit
let instantiateMyDynamicType (domain: AppDomain) =
try
// You must supply a valid fully qualified assembly name here.
domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType")
|> ignore
with e ->
printfn $"{e.Message}"
let defineDynamicAssembly (domain: AppDomain) =
// Build a dynamic assembly using Reflection Emit API.
let assemblyName = AssemblyName()
assemblyName.Name <- "MyDynamicAssembly"
let assemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run)
let moduleBuilder = assemblyBuilder.DefineDynamicModule "MyDynamicModule"
let typeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public)
let constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, null)
let ilGenerator = constructorBuilder.GetILGenerator()
ilGenerator.EmitWriteLine "MyDynamicType instantiated!"
ilGenerator.Emit OpCodes.Ret
typeBuilder.CreateType() |> ignore
assemblyBuilder
let myResolveEventHandler (sender: obj) _ =
defineDynamicAssembly (sender :?> AppDomain)
:> Assembly
let currentDomain = AppDomain.CurrentDomain
instantiateMyDynamicType currentDomain // Failed!
currentDomain.add_AssemblyResolve(ResolveEventHandler myResolveEventHandler)
instantiateMyDynamicType currentDomain // OK!
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
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
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
注解
此方法仅用于定义当前应用程序域中的动态程序集。 有关详细信息,请参阅 Load(AssemblyName) 方法重载。
注意
在开发发出动态程序集的代码期间,建议使用 方法的 DefineDynamicAssembly 重载来指定证据和权限,提供希望动态程序集具有的证据,并包含在 SecurityPermissionFlag.SkipVerification 中 refusedPermissions
。 在 SkipVerification 参数中包含 refusedPermissions
可确保验证 MSIL。 此方法的一个限制是,当与需要完全信任的代码一起使用时,它也会导致 SecurityException 引发。