System.AppDomain.DefineDynamicAssembly 方法 (AssemblyName, AssemblyBuilderAccess, IEnumerable)
方法描述
使用指定的名称、访问模式和自定义特性定义动态程序集。
语法定义(C# System.AppDomain.DefineDynamicAssembly 方法 (AssemblyName, AssemblyBuilderAccess, IEnumerable) 的用法)
public AssemblyBuilder DefineDynamicAssembly( AssemblyName name, AssemblyBuilderAccess access, IEnumerableassemblyAttributes )
参数/返回值
| 参数值/返回值 | 参数类型/返回类型 | 参数描述/返回描述 |
|---|---|---|
| name | System-Reflection-AssemblyName | 动态程序集的唯一标识。 |
| access | System-Reflection-Emit-AssemblyBuilderAccess | 动态程序集的访问模式。 |
| assemblyAttributes | System-Collections-Generic-IEnumerable |
要应用于程序集的可枚举特性列表;如果无特性,则为 null。 |
| 返回值 | System.Reflection.Emit.AssemblyBuilder | 具有指定名称和功能的动态程序集。 |
提示和注释
使用此方法重载指定除非在创建动态程序集时已应用,否则无法正常使用的特性。 例如,如果在创建动态程序集后添加了安全特性(如 SecurityTransparentAttribute 和 SecurityCriticalAttribute),则这些特性无法正常使用。
此方法只应用于在当前应用程序域中定义动态程序集。 有关此限制的更多信息,请参见 Load(AssemblyName) 方法重载。
.NET Framework 3.5 版 中引入了此方法重载。
System.AppDomain.DefineDynamicAssembly 方法 (AssemblyName, AssemblyBuilderAccess, IEnumerable)例子
代码示例在新的动态程序集内定义一个模块和一个类型,然后显示程序集的特性。
using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Security;
class Example
{
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
*/
异常
| 异常 | 异常描述 |
|---|---|
| ArgumentNullException | name 为 null。 |
| ArgumentException |
|
| AppDomainUnloadedException | 尝试对已卸载的应用程序域进行操作。 |
版本信息
.NET Framework 受以下版本支持:4、3.5 SP1、3.0 SP1、2.0 SP1 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1
适用平台
Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。