某方法只要属于泛型类型,且使用该类型的类型参数,就不是泛型方法。 只有当方法有属于自己的类型参数列表时才是泛型方法。 多数泛型类型上的方法都不是泛型方法,如本示例所示。 有关发出泛型方法的示例,请参阅
如何:使用反射发出定义泛型方法
。
定义名为
GenericEmitExample1
的动态程序集。 在此示例中,执行了程序集并将其保存到磁盘中,所以指定了
AssemblyBuilderAccess.RunAndSave
。
AppDomain^ myDomain = AppDomain::CurrentDomain;
AssemblyName^ myAsmName = gcnew AssemblyName( L"GenericEmitExample1" );
AssemblyBuilder^ myAssembly = myDomain->DefineDynamicAssembly(
myAsmName, AssemblyBuilderAccess::RunAndSave );
AppDomain myDomain = AppDomain.CurrentDomain;
AssemblyName myAsmName = new AssemblyName("GenericEmitExample1");
AssemblyBuilder myAssembly =
myDomain.DefineDynamicAssembly(myAsmName,
AssemblyBuilderAccess.RunAndSave);
Dim myDomain As AppDomain = AppDomain.CurrentDomain
Dim myAsmName As New AssemblyName("GenericEmitExample1")
Dim myAssembly As AssemblyBuilder = myDomain.DefineDynamicAssembly( _
myAsmName, _
AssemblyBuilderAccess.RunAndSave)
定义动态模块。 程序集由可执行模块组成。 对于单模块程序集,模块名称与程序集名称相同,文件名为模块名称加上扩展名。
ModuleBuilder^ myModule = myAssembly->DefineDynamicModule(
myAsmName->Name, String::Concat( myAsmName->Name, L".dll" ) );
ModuleBuilder myModule =
myAssembly.DefineDynamicModule(myAsmName.Name,
myAsmName.Name + ".dll");
Dim myModule As ModuleBuilder = myAssembly.DefineDynamicModule( _
myAsmName.Name, _
myAsmName.Name & ".dll")
定义类。 在此示例中,类命名为 Sample
。
TypeBuilder^ myType = myModule->DefineType( L"Sample",
TypeAttributes::Public );
TypeBuilder myType =
myModule.DefineType("Sample", TypeAttributes.Public);
Dim myType As TypeBuilder = myModule.DefineType( _
"Sample", _
TypeAttributes.Public)
通过将包含参数名称的字符串数组传递给 TypeBuilder.DefineGenericParameters 方法来定义 Sample
的泛型类型参数。 这将使该类成为泛型类型。 返回值是表示类型参数的 GenericTypeParameterBuilder 对象数组,可用于发出的代码。
在以下代码中,Sample
将成为具有类型参数 TFirst
和 TSecond
的泛型类型。 为了使代码更易于阅读,每个 GenericTypeParameterBuilder 都将放置在与类型参数同名的变量中。
array<String^>^typeParamNames = {L"TFirst",L"TSecond"};
array<GenericTypeParameterBuilder^>^typeParams =
myType->DefineGenericParameters( typeParamNames );
GenericTypeParameterBuilder^ TFirst = typeParams[0];
GenericTypeParameterBuilder^ TSecond = typeParams[1];
string[] typeParamNames = {"TFirst", "TSecond"};
GenericTypeParameterBuilder[] typeParams =
myType.DefineGenericParameters(typeParamNames);
GenericTypeParameterBuilder TFirst = typeParams[0];
GenericTypeParameterBuilder TSecond = typeParams[1];
Dim typeParamNames() As String = {"TFirst", "TSecond"}
Dim typeParams() As GenericTypeParameterBuilder = _
myType.DefineGenericParameters(typeParamNames)
Dim TFirst As GenericTypeParameterBuilder = typeParams(0)
Dim TSecond As GenericTypeParameterBuilder = typeParams(1)
向类型参数添加特殊约束。 在此示例中,类型参数 TFirst
被限制为具有无参数构造函数的类型以及引用类型。
TFirst->SetGenericParameterAttributes(
GenericParameterAttributes::DefaultConstructorConstraint |
GenericParameterAttributes::ReferenceTypeConstraint
TFirst.SetGenericParameterAttributes(
GenericParameterAttributes.DefaultConstructorConstraint |
GenericParameterAttributes.ReferenceTypeConstraint);
TFirst.SetGenericParameterAttributes( _
GenericParameterAttributes.DefaultConstructorConstraint _
Or GenericParameterAttributes.ReferenceTypeConstraint)
可以选择向类型参数添加类约束和接口约束。 在此示例中,类型参数 TFirst
被限制为从 baseType
变量包含的 Type 对象所表示的基类中派生的类型,以及实现接口类型包含在变量 interfaceA
和 interfaceB
中的接口的类型。 有关这些变量的声明和分配,请参阅代码示例。
array<Type^>^interfaceTypes = { interfaceA, interfaceB };
TSecond->SetInterfaceConstraints( interfaceTypes );
TSecond->SetBaseTypeConstraint( baseType );
TSecond.SetBaseTypeConstraint(baseType);
Type[] interfaceTypes = {interfaceA, interfaceB};
TSecond.SetInterfaceConstraints(interfaceTypes);
TSecond.SetBaseTypeConstraint(baseType)
Dim interfaceTypes() As Type = {interfaceA, interfaceB}
TSecond.SetInterfaceConstraints(interfaceTypes)
定义字段。 在此示例中,该字段的类型由类型参数 TFirst
指定。 GenericTypeParameterBuilder 派生自 Type,因此可在任何能够使用类型的位置使用泛型类型参数。
FieldBuilder^ exField =
myType->DefineField("ExampleField", TFirst,
FieldAttributes::Private);
FieldBuilder exField =
myType.DefineField("ExampleField", TFirst,
FieldAttributes.Private);
Dim exField As FieldBuilder = _
myType.DefineField("ExampleField", TFirst, _
FieldAttributes.Private)
定义使用泛型类型的类型参数的方法。 请注意,此类方法不是泛型方法,除非它们具有自己的类型参数列表。 以下代码定义一个 static
方法(在 Visual Basic 中为 Shared
),该方法接收一个 TFirst
数组并返回包含该数组所有元素的 List<TFirst>
(在 Visual Basic 中为 List(Of TFirst)
)。 若要定义此方法,需要通过调用泛型类型定义 List<T>
上的 MakeGenericType 来创建类型 List<TFirst>
。 (使用 typeof
运算符(在 Visual Basic 中为 GetType
)获取泛型类型定义时,将忽略 T
。) 通过使用 MakeArrayType 方法创建参数类型。
Type^ listOf = List::typeid;
Type^ listOfTFirst = listOf->MakeGenericType(TFirst);
array<Type^>^ mParamTypes = { TFirst->MakeArrayType() };
MethodBuilder^ exMethod =
myType->DefineMethod("ExampleMethod",
MethodAttributes::Public | MethodAttributes::Static,
listOfTFirst,
mParamTypes);
Type listOf = typeof(List<>);
Type listOfTFirst = listOf.MakeGenericType(TFirst);
Type[] mParamTypes = {TFirst.MakeArrayType()};
MethodBuilder exMethod =
myType.DefineMethod("ExampleMethod",
MethodAttributes.Public | MethodAttributes.Static,
listOfTFirst,
mParamTypes);
Dim listOf As Type = GetType(List(Of ))
Dim listOfTFirst As Type = listOf.MakeGenericType(TFirst)
Dim mParamTypes() As Type = {TFirst.MakeArrayType()}
Dim exMethod As MethodBuilder = _
myType.DefineMethod("ExampleMethod", _
MethodAttributes.Public Or MethodAttributes.Static, _
listOfTFirst, _
mParamTypes)
发出方法主体。 方法主体由三个操作码组成,这些操作码将输入数组加载到堆栈,调用接收 IEnumerable<TFirst>
的 List<TFirst>
构造函数(该函数完成将输入元素放入列表的所有工作)并返回(将新的 List<T> 对象留在堆栈中)。 发出此代码的难点在于获取构造函数。
GenericTypeParameterBuilder 中不支持 GetConstructor 方法,所以不可能直接获取 List<TFirst>
的构造函数。 首先,必须获取泛型类型定义 List<T>
的构造函数,然后调用一种可将其转换为 List<TFirst>
的对应构造函数的方法。
用于此代码示例的构造函数接收 IEnumerable<T>
。 但请注意,这不是 IEnumerable<T> 泛型接口的泛型类型定义;相反,必须将 List<T>
的类型参数 T
替代为 IEnumerable<T>
的类型参数 T
。 (这似乎容易混淆,因为这两个类型都具有名为 T
的类型参数。这就是此代码示例使用名称 TFirst
和 TSecond
的原因。)若要获取构造函数的参数类型,请从泛型类型定义 IEnumerable<T>
开始,并使用 MakeGenericType 的第一个泛型类型参数调用 List<T>
。 构造函数参数列表必须作为数组进行传递,在本例中只有一个参数。
在 C# 中使用 typeof
运算符时,泛型类型定义将表达为 IEnumerable<>
;在 Visual Basic 中使用 GetType
运算符时,泛型类型定义将表达为 IEnumerable(Of )
。
现在,可以通过调用泛型类型定义上的 GetConstructor 来获取 List<T>
的构造函数。 若要将此构造函数转换为 List<TFirst>
对应的构造函数,请将 List<TFirst>
和 List<T>
的构造函数传递给静态 TypeBuilder.GetConstructor(Type, ConstructorInfo) 方法。
ILGenerator^ ilgen = exMethod->GetILGenerator();
Type^ ienumOf = IEnumerable::typeid;
Type^ TfromListOf = listOf->GetGenericArguments()[0];
Type^ ienumOfT = ienumOf->MakeGenericType(TfromListOf);
array<Type^>^ ctorArgs = {ienumOfT};
ConstructorInfo^ ctorPrep = listOf->GetConstructor(ctorArgs);
ConstructorInfo^ ctor =
TypeBuilder::GetConstructor(listOfTFirst, ctorPrep);
ilgen->Emit(OpCodes::Ldarg_0);
ilgen->Emit(OpCodes::Newobj, ctor);
ilgen->Emit(OpCodes::Ret);
ILGenerator ilgen = exMethod.GetILGenerator();
Type ienumOf = typeof(IEnumerable<>);
Type TfromListOf = listOf.GetGenericArguments()[0];
Type ienumOfT = ienumOf.MakeGenericType(TfromListOf);
Type[] ctorArgs = {ienumOfT};
ConstructorInfo ctorPrep = listOf.GetConstructor(ctorArgs);
ConstructorInfo ctor =
TypeBuilder.GetConstructor(listOfTFirst, ctorPrep);
ilgen.Emit(OpCodes.Ldarg_0);
ilgen.Emit(OpCodes.Newobj, ctor);
ilgen.Emit(OpCodes.Ret);
Dim ilgen As ILGenerator = exMethod.GetILGenerator()
Dim ienumOf As Type = GetType(IEnumerable(Of ))
Dim listOfTParams() As Type = listOf.GetGenericArguments()
Dim TfromListOf As Type = listOfTParams(0)
Dim ienumOfT As Type = ienumOf.MakeGenericType(TfromListOf)
Dim ctorArgs() As Type = {ienumOfT}
Dim ctorPrep As ConstructorInfo = _
listOf.GetConstructor(ctorArgs)
Dim ctor As ConstructorInfo = _
TypeBuilder.GetConstructor(listOfTFirst, ctorPrep)
ilgen.Emit(OpCodes.Ldarg_0)
ilgen.Emit(OpCodes.Newobj, ctor)
ilgen.Emit(OpCodes.Ret)
创建类型,并保存文件。
Type^ finished = myType->CreateType();
myAssembly->Save( String::Concat( myAsmName->Name, L".dll" ) );
Type finished = myType.CreateType();
myAssembly.Save(myAsmName.Name+".dll");
Dim finished As Type = myType.CreateType()
myAssembly.Save(myAsmName.Name & ".dll")
调用方法。 ExampleMethod
不是泛型,但它所属的类型为泛型。因此,为了获取可以调用的 MethodInfo,有必要从 Sample
的类型定义创建构造类型。 该构造类型使用 Example
类和 ExampleDerived
类,前者满足 TFirst
上的约束,因为它是引用类型并且具有默认的无参数构造函数,后者满足 TSecond
上的约束。 (示例代码部分提供了 ExampleDerived
的代码。)将这两个类型传递给 MakeGenericType 以创建构造类型。 然后使用 GetMethod 方法获取 MethodInfo。
array<Type^>^ typeArgs =
{ Example::typeid, ExampleDerived::typeid };
Type^ constructed = finished->MakeGenericType(typeArgs);
MethodInfo^ mi = constructed->GetMethod("ExampleMethod");
Type[] typeArgs = {typeof(Example), typeof(ExampleDerived)};
Type constructed = finished.MakeGenericType(typeArgs);
MethodInfo mi = constructed.GetMethod("ExampleMethod");
Dim typeArgs() As Type = _
{GetType(Example), GetType(ExampleDerived)}
Dim constructed As Type = finished.MakeGenericType(typeArgs)
Dim mi As MethodInfo = constructed.GetMethod("ExampleMethod")
以下代码将创建一个 Example
对象数组,将该数组放在类型为 Object,表示要调用的方法的参数的数组中,然后将创建的数组传递给 Invoke(Object, Object[]) 方法。 Invoke 方法的第一个参数是空引用,因为该方法为 static
。
array<Example^>^ input = { gcnew Example(), gcnew Example() };
array<Object^>^ arguments = { input };
List<Example^>^ listX =
(List<Example^>^) mi->Invoke(nullptr, arguments);
Console::WriteLine(
"\nThere are {0} elements in the List<Example>.",
listX->Count);
Example[] input = {new Example(), new Example()};
object[] arguments = {input};
List<Example> listX =
(List<Example>) mi.Invoke(null, arguments);
Console.WriteLine(
"\nThere are {0} elements in the List<Example>.",
listX.Count);
Dim input() As Example = {New Example(), New Example()}
Dim arguments() As Object = {input}
Dim listX As List(Of Example) = mi.Invoke(Nothing, arguments)
Console.WriteLine(vbLf & _
"There are {0} elements in the List(Of Example).", _
listX.Count _
以下代码示例定义一个名为 Sample
的类,还定义一个基类和两个接口。 该程序可为 Sample
定义两个泛型类型参数,使其成为泛型类型。 只有类型参数能使一个类型成为泛型类型。 该程序通过显示定义类型参数前后的测试消息来对此进行演示。
类型参数 TSecond
用于通过基类和接口来演示类和接口约束,类型参数 TFirst
用于演示特殊约束。
此代码示例针对字段类型以及方法的参数和返回类型,通过使用该类的类型参数定义字段和方法。
创建 Sample
类后,调用该方法。
该程序中包括一个可列出泛型类型信息的方法,和一个可列出类型参数上的特殊约束的方法。 这些方法用于显示有关已完成的 Sample
类的信息。
程序将完成的模块以 GenericEmitExample1.dll
的形式保存到磁盘中,因此可以使用 Ildasm.exe(IL 反汇编程序)将其打开,并检查 Sample
类的 CIL。
using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
using namespace System::Collections::Generic;
// Dummy class to satisfy TFirst constraints.
public ref class Example {};
// Define a trivial base class and two trivial interfaces
// to use when demonstrating constraints.
public ref class ExampleBase {};
public interface class IExampleA {};
public interface class IExampleB {};
// Define a trivial type that can substitute for type parameter
// TSecond.
public ref class ExampleDerived : ExampleBase, IExampleA, IExampleB {};
// List the constraint flags. The GenericParameterAttributes
// enumeration contains two sets of attributes, variance and
// constraints. For this example, only constraints are used.
static void ListConstraintAttributes( Type^ t )
// Mask off the constraint flags.
GenericParameterAttributes constraints =
t->GenericParameterAttributes &
GenericParameterAttributes::SpecialConstraintMask;
if ((constraints & GenericParameterAttributes::ReferenceTypeConstraint)
!= GenericParameterAttributes::None)
Console::WriteLine( L" ReferenceTypeConstraint");
if ((constraints & GenericParameterAttributes::NotNullableValueTypeConstraint)
!= GenericParameterAttributes::None)
Console::WriteLine( L" NotNullableValueTypeConstraint");
if ((constraints & GenericParameterAttributes::DefaultConstructorConstraint)
!= GenericParameterAttributes::None)
Console::WriteLine( L" DefaultConstructorConstraint");
static void DisplayGenericParameters( Type^ t )
if (!t->IsGenericType)
Console::WriteLine( L"Type '{0}' is not generic." );
return;
if (!t->IsGenericTypeDefinition)
t = t->GetGenericTypeDefinition();
array<Type^>^ typeParameters = t->GetGenericArguments();
Console::WriteLine( L"\r\nListing {0} type parameters for type '{1}'.",
typeParameters->Length, t );
for each ( Type^ tParam in typeParameters )
Console::WriteLine( L"\r\nType parameter {0}:",
tParam->ToString() );
for each (Type^ c in tParam->GetGenericParameterConstraints())
if (c->IsInterface)
Console::WriteLine( L" Interface constraint: {0}", c);
Console::WriteLine( L" Base type constraint: {0}", c);
ListConstraintAttributes(tParam);
void main()
// Define a dynamic assembly to contain the sample type. The
// assembly will be run and also saved to disk, so
// AssemblyBuilderAccess.RunAndSave is specified.
AppDomain^ myDomain = AppDomain::CurrentDomain;
AssemblyName^ myAsmName = gcnew AssemblyName( L"GenericEmitExample1" );
AssemblyBuilder^ myAssembly = myDomain->DefineDynamicAssembly(
myAsmName, AssemblyBuilderAccess::RunAndSave );
// An assembly is made up of executable modules. For a single-
// module assembly, the module name and file name are the same
// as the assembly name.
ModuleBuilder^ myModule = myAssembly->DefineDynamicModule(
myAsmName->Name, String::Concat( myAsmName->Name, L".dll" ) );
// Get type objects for the base class trivial interfaces to
// be used as constraints.
Type^ baseType = ExampleBase::typeid;
Type^ interfaceA = IExampleA::typeid;
Type^ interfaceB = IExampleB::typeid;
// Define the sample type.
TypeBuilder^ myType = myModule->DefineType( L"Sample",
TypeAttributes::Public );
Console::WriteLine( L"Type 'Sample' is generic: {0}",
myType->IsGenericType );
// Define type parameters for the type. Until you do this,
// the type is not generic, as the preceding and following
// WriteLine statements show. The type parameter names are
// specified as an array of strings. To make the code
// easier to read, each GenericTypeParameterBuilder is placed
// in a variable with the same name as the type parameter.
array<String^>^typeParamNames = {L"TFirst",L"TSecond"};
array<GenericTypeParameterBuilder^>^typeParams =
myType->DefineGenericParameters( typeParamNames );
GenericTypeParameterBuilder^ TFirst = typeParams[0];
GenericTypeParameterBuilder^ TSecond = typeParams[1];
Console::WriteLine( L"Type 'Sample' is generic: {0}",
myType->IsGenericType );
// Apply constraints to the type parameters.
// A type that is substituted for the first parameter, TFirst,
// must be a reference type and must have a parameterless
// constructor.
TFirst->SetGenericParameterAttributes(
GenericParameterAttributes::DefaultConstructorConstraint |
GenericParameterAttributes::ReferenceTypeConstraint
// A type that is substituted for the second type
// parameter must implement IExampleA and IExampleB, and
// inherit from the trivial test class ExampleBase. The
// interface constraints are specified as an array
// containing the interface types.
array<Type^>^interfaceTypes = { interfaceA, interfaceB };
TSecond->SetInterfaceConstraints( interfaceTypes );
TSecond->SetBaseTypeConstraint( baseType );
// The following code adds a private field named ExampleField,
// of type TFirst.
FieldBuilder^ exField =
myType->DefineField("ExampleField", TFirst,
FieldAttributes::Private);
// Define a static method that takes an array of TFirst and
// returns a List<TFirst> containing all the elements of
// the array. To define this method it is necessary to create
// the type List<TFirst> by calling MakeGenericType on the
// generic type definition, generic<T> List.
// The parameter type is created by using the
// MakeArrayType method.
Type^ listOf = List::typeid;
Type^ listOfTFirst = listOf->MakeGenericType(TFirst);
array<Type^>^ mParamTypes = { TFirst->MakeArrayType() };
MethodBuilder^ exMethod =
myType->DefineMethod("ExampleMethod",
MethodAttributes::Public | MethodAttributes::Static,
listOfTFirst,
mParamTypes);
// Emit the method body.
// The method body consists of just three opcodes, to load
// the input array onto the execution stack, to call the
// List<TFirst> constructor that takes IEnumerable<TFirst>,
// which does all the work of putting the input elements into
// the list, and to return, leaving the list on the stack. The
// hard work is getting the constructor.
// The GetConstructor method is not supported on a
// GenericTypeParameterBuilder, so it is not possible to get
// the constructor of List<TFirst> directly. There are two
// steps, first getting the constructor of generic<T> List and then
// calling a method that converts it to the corresponding
// constructor of List<TFirst>.
// The constructor needed here is the one that takes an
// IEnumerable<T>. Note, however, that this is not the
// generic type definition of generic<T> IEnumerable; instead, the
// T from generic<T> List must be substituted for the T of
// generic<T> IEnumerable. (This seems confusing only because both
// types have type parameters named T. That is why this example
// uses the somewhat silly names TFirst and TSecond.) To get
// the type of the constructor argument, take the generic
// type definition generic<T> IEnumerable and
// call MakeGenericType with the first generic type parameter
// of generic<T> List. The constructor argument list must be passed
// as an array, with just one argument in this case.
// Now it is possible to get the constructor of generic<T> List,
// using GetConstructor on the generic type definition. To get
// the constructor of List<TFirst>, pass List<TFirst> and
// the constructor from generic<T> List to the static
// TypeBuilder.GetConstructor method.
ILGenerator^ ilgen = exMethod->GetILGenerator();
Type^ ienumOf = IEnumerable::typeid;
Type^ TfromListOf = listOf->GetGenericArguments()[0];
Type^ ienumOfT = ienumOf->MakeGenericType(TfromListOf);
array<Type^>^ ctorArgs = {ienumOfT};
ConstructorInfo^ ctorPrep = listOf->GetConstructor(ctorArgs);
ConstructorInfo^ ctor =
TypeBuilder::GetConstructor(listOfTFirst, ctorPrep);
ilgen->Emit(OpCodes::Ldarg_0);
ilgen->Emit(OpCodes::Newobj, ctor);
ilgen->Emit(OpCodes::Ret);
// Create the type and save the assembly.
Type^ finished = myType->CreateType();
myAssembly->Save( String::Concat( myAsmName->Name, L".dll" ) );
// Invoke the method.
// ExampleMethod is not generic, but the type it belongs to is
// generic, so in order to get a MethodInfo that can be invoked
// it is necessary to create a constructed type. The Example
// class satisfies the constraints on TFirst, because it is a
// reference type and has a default constructor. In order to
// have a class that satisfies the constraints on TSecond,
// this code example defines the ExampleDerived type. These
// two types are passed to MakeGenericMethod to create the
// constructed type.
array<Type^>^ typeArgs =
{ Example::typeid, ExampleDerived::typeid };
Type^ constructed = finished->MakeGenericType(typeArgs);
MethodInfo^ mi = constructed->GetMethod("ExampleMethod");
// Create an array of Example objects, as input to the generic
// method. This array must be passed as the only element of an
// array of arguments. The first argument of Invoke is
// null, because ExampleMethod is static. Display the count
// on the resulting List<Example>.
array<Example^>^ input = { gcnew Example(), gcnew Example() };
array<Object^>^ arguments = { input };
List<Example^>^ listX =
(List<Example^>^) mi->Invoke(nullptr, arguments);
Console::WriteLine(
"\nThere are {0} elements in the List<Example>.",
listX->Count);
DisplayGenericParameters(finished);
/* This code example produces the following output:
Type 'Sample' is generic: False
Type 'Sample' is generic: True
There are 2 elements in the List<Example>.
Listing 2 type parameters for type 'Sample[TFirst,TSecond]'.
Type parameter TFirst:
ReferenceTypeConstraint
DefaultConstructorConstraint
Type parameter TSecond:
Interface constraint: IExampleA
Interface constraint: IExampleB
Base type constraint: ExampleBase
using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Collections.Generic;
// Define a trivial base class and two trivial interfaces
// to use when demonstrating constraints.
public class ExampleBase {}
public interface IExampleA {}
public interface IExampleB {}
// Define a trivial type that can substitute for type parameter
// TSecond.
public class ExampleDerived : ExampleBase, IExampleA, IExampleB {}
public class Example
public static void Main()
// Define a dynamic assembly to contain the sample type. The
// assembly will not be run, but only saved to disk, so
// AssemblyBuilderAccess.Save is specified.
AppDomain myDomain = AppDomain.CurrentDomain;
AssemblyName myAsmName = new AssemblyName("GenericEmitExample1");
AssemblyBuilder myAssembly =
myDomain.DefineDynamicAssembly(myAsmName,
AssemblyBuilderAccess.RunAndSave);
// An assembly is made up of executable modules. For a single-
// module assembly, the module name and file name are the same
// as the assembly name.
ModuleBuilder myModule =
myAssembly.DefineDynamicModule(myAsmName.Name,
myAsmName.Name + ".dll");
// Get type objects for the base class trivial interfaces to
// be used as constraints.
Type baseType = typeof(ExampleBase);
Type interfaceA = typeof(IExampleA);
Type interfaceB = typeof(IExampleB);
// Define the sample type.
TypeBuilder myType =
myModule.DefineType("Sample", TypeAttributes.Public);
Console.WriteLine("Type 'Sample' is generic: {0}",
myType.IsGenericType);
// Define type parameters for the type. Until you do this,
// the type is not generic, as the preceding and following
// WriteLine statements show. The type parameter names are
// specified as an array of strings. To make the code
// easier to read, each GenericTypeParameterBuilder is placed
// in a variable with the same name as the type parameter.
string[] typeParamNames = {"TFirst", "TSecond"};
GenericTypeParameterBuilder[] typeParams =
myType.DefineGenericParameters(typeParamNames);
GenericTypeParameterBuilder TFirst = typeParams[0];
GenericTypeParameterBuilder TSecond = typeParams[1];
Console.WriteLine("Type 'Sample' is generic: {0}",
myType.IsGenericType);
// Apply constraints to the type parameters.
// A type that is substituted for the first parameter, TFirst,
// must be a reference type and must have a parameterless
// constructor.
TFirst.SetGenericParameterAttributes(
GenericParameterAttributes.DefaultConstructorConstraint |
GenericParameterAttributes.ReferenceTypeConstraint);
// A type that is substituted for the second type
// parameter must implement IExampleA and IExampleB, and
// inherit from the trivial test class ExampleBase. The
// interface constraints are specified as an array
// containing the interface types.
TSecond.SetBaseTypeConstraint(baseType);
Type[] interfaceTypes = {interfaceA, interfaceB};
TSecond.SetInterfaceConstraints(interfaceTypes);
// The following code adds a private field named ExampleField,
// of type TFirst.
FieldBuilder exField =
myType.DefineField("ExampleField", TFirst,
FieldAttributes.Private);
// Define a static method that takes an array of TFirst and
// returns a List<TFirst> containing all the elements of
// the array. To define this method it is necessary to create
// the type List<TFirst> by calling MakeGenericType on the
// generic type definition, List<T>. (The T is omitted with
// the typeof operator when you get the generic type
// definition.) The parameter type is created by using the
// MakeArrayType method.
Type listOf = typeof(List<>);
Type listOfTFirst = listOf.MakeGenericType(TFirst);
Type[] mParamTypes = {TFirst.MakeArrayType()};
MethodBuilder exMethod =
myType.DefineMethod("ExampleMethod",
MethodAttributes.Public | MethodAttributes.Static,
listOfTFirst,
mParamTypes);
// Emit the method body.
// The method body consists of just three opcodes, to load
// the input array onto the execution stack, to call the
// List<TFirst> constructor that takes IEnumerable<TFirst>,
// which does all the work of putting the input elements into
// the list, and to return, leaving the list on the stack. The
// hard work is getting the constructor.
// The GetConstructor method is not supported on a
// GenericTypeParameterBuilder, so it is not possible to get
// the constructor of List<TFirst> directly. There are two
// steps, first getting the constructor of List<T> and then
// calling a method that converts it to the corresponding
// constructor of List<TFirst>.
// The constructor needed here is the one that takes an
// IEnumerable<T>. Note, however, that this is not the
// generic type definition of IEnumerable<T>; instead, the
// T from List<T> must be substituted for the T of
// IEnumerable<T>. (This seems confusing only because both
// types have type parameters named T. That is why this example
// uses the somewhat silly names TFirst and TSecond.) To get
// the type of the constructor argument, take the generic
// type definition IEnumerable<T> (expressed as
// IEnumerable<> when you use the typeof operator) and
// call MakeGenericType with the first generic type parameter
// of List<T>. The constructor argument list must be passed
// as an array, with just one argument in this case.
// Now it is possible to get the constructor of List<T>,
// using GetConstructor on the generic type definition. To get
// the constructor of List<TFirst>, pass List<TFirst> and
// the constructor from List<T> to the static
// TypeBuilder.GetConstructor method.
ILGenerator ilgen = exMethod.GetILGenerator();
Type ienumOf = typeof(IEnumerable<>);
Type TfromListOf = listOf.GetGenericArguments()[0];
Type ienumOfT = ienumOf.MakeGenericType(TfromListOf);
Type[] ctorArgs = {ienumOfT};
ConstructorInfo ctorPrep = listOf.GetConstructor(ctorArgs);
ConstructorInfo ctor =
TypeBuilder.GetConstructor(listOfTFirst, ctorPrep);
ilgen.Emit(OpCodes.Ldarg_0);
ilgen.Emit(OpCodes.Newobj, ctor);
ilgen.Emit(OpCodes.Ret);
// Create the type and save the assembly.
Type finished = myType.CreateType();
myAssembly.Save(myAsmName.Name+".dll");
// Invoke the method.
// ExampleMethod is not generic, but the type it belongs to is
// generic, so in order to get a MethodInfo that can be invoked
// it is necessary to create a constructed type. The Example
// class satisfies the constraints on TFirst, because it is a
// reference type and has a default constructor. In order to
// have a class that satisfies the constraints on TSecond,
// this code example defines the ExampleDerived type. These
// two types are passed to MakeGenericMethod to create the
// constructed type.
Type[] typeArgs = {typeof(Example), typeof(ExampleDerived)};
Type constructed = finished.MakeGenericType(typeArgs);
MethodInfo mi = constructed.GetMethod("ExampleMethod");
// Create an array of Example objects, as input to the generic
// method. This array must be passed as the only element of an
// array of arguments. The first argument of Invoke is
// null, because ExampleMethod is static. Display the count
// on the resulting List<Example>.
Example[] input = {new Example(), new Example()};
object[] arguments = {input};
List<Example> listX =
(List<Example>) mi.Invoke(null, arguments);
Console.WriteLine(
"\nThere are {0} elements in the List<Example>.",
listX.Count);
DisplayGenericParameters(finished);
private static void DisplayGenericParameters(Type t)
if (!t.IsGenericType)
Console.WriteLine("Type '{0}' is not generic.");
return;
if (!t.IsGenericTypeDefinition)
t = t.GetGenericTypeDefinition();
Type[] typeParameters = t.GetGenericArguments();
Console.WriteLine("\nListing {0} type parameters for type '{1}'.",
typeParameters.Length, t);
foreach( Type tParam in typeParameters )
Console.WriteLine("\r\nType parameter {0}:", tParam.ToString());
foreach( Type c in tParam.GetGenericParameterConstraints() )
if (c.IsInterface)
Console.WriteLine(" Interface constraint: {0}", c);
Console.WriteLine(" Base type constraint: {0}", c);
ListConstraintAttributes(tParam);
// List the constraint flags. The GenericParameterAttributes
// enumeration contains two sets of attributes, variance and
// constraints. For this example, only constraints are used.
private static void ListConstraintAttributes(Type t)
// Mask off the constraint flags.
GenericParameterAttributes constraints =
t.GenericParameterAttributes & GenericParameterAttributes.SpecialConstraintMask;
if ((constraints & GenericParameterAttributes.ReferenceTypeConstraint)
!= GenericParameterAttributes.None)
Console.WriteLine(" ReferenceTypeConstraint");
if ((constraints & GenericParameterAttributes.NotNullableValueTypeConstraint)
!= GenericParameterAttributes.None)
Console.WriteLine(" NotNullableValueTypeConstraint");
if ((constraints & GenericParameterAttributes.DefaultConstructorConstraint)
!=GenericParameterAttributes.None)
Console.WriteLine(" DefaultConstructorConstraint");
/* This code example produces the following output:
Type 'Sample' is generic: False
Type 'Sample' is generic: True
There are 2 elements in the List<Example>.
Listing 2 type parameters for type 'Sample[TFirst,TSecond]'.
Type parameter TFirst:
ReferenceTypeConstraint
DefaultConstructorConstraint
Type parameter TSecond:
Interface constraint: IExampleA
Interface constraint: IExampleB
Base type constraint: ExampleBase
Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Collections.Generic
' Define a trivial base class and two trivial interfaces
' to use when demonstrating constraints.
Public Class ExampleBase
End Class
Public Interface IExampleA
End Interface
Public Interface IExampleB
End Interface
' Define a trivial type that can substitute for type parameter
' TSecond.
Public Class ExampleDerived
Inherits ExampleBase
Implements IExampleA, IExampleB
End Class
Public Class Example
Public Shared Sub Main()
' Define a dynamic assembly to contain the sample type. The
' assembly will not be run, but only saved to disk, so
' AssemblyBuilderAccess.Save is specified.
Dim myDomain As AppDomain = AppDomain.CurrentDomain
Dim myAsmName As New AssemblyName("GenericEmitExample1")
Dim myAssembly As AssemblyBuilder = myDomain.DefineDynamicAssembly( _
myAsmName, _
AssemblyBuilderAccess.RunAndSave)
' An assembly is made up of executable modules. For a single-
' module assembly, the module name and file name are the same
' as the assembly name.
Dim myModule As ModuleBuilder = myAssembly.DefineDynamicModule( _
myAsmName.Name, _
myAsmName.Name & ".dll")
' Get type objects for the base class trivial interfaces to
' be used as constraints.
Dim baseType As Type = GetType(ExampleBase)
Dim interfaceA As Type = GetType(IExampleA)
Dim interfaceB As Type = GetType(IExampleB)
' Define the sample type.
Dim myType As TypeBuilder = myModule.DefineType( _
"Sample", _
TypeAttributes.Public)
Console.WriteLine("Type 'Sample' is generic: {0}", _
myType.IsGenericType)
' Define type parameters for the type. Until you do this,
' the type is not generic, as the preceding and following
' WriteLine statements show. The type parameter names are
' specified as an array of strings. To make the code
' easier to read, each GenericTypeParameterBuilder is placed
' in a variable with the same name as the type parameter.
Dim typeParamNames() As String = {"TFirst", "TSecond"}
Dim typeParams() As GenericTypeParameterBuilder = _
myType.DefineGenericParameters(typeParamNames)
Dim TFirst As GenericTypeParameterBuilder = typeParams(0)
Dim TSecond As GenericTypeParameterBuilder = typeParams(1)
Console.WriteLine("Type 'Sample' is generic: {0}", _
myType.IsGenericType)
' Apply constraints to the type parameters.
' A type that is substituted for the first parameter, TFirst,
' must be a reference type and must have a parameterless
' constructor.
TFirst.SetGenericParameterAttributes( _
GenericParameterAttributes.DefaultConstructorConstraint _
Or GenericParameterAttributes.ReferenceTypeConstraint)
' A type that is substituted for the second type
' parameter must implement IExampleA and IExampleB, and
' inherit from the trivial test class ExampleBase. The
' interface constraints are specified as an array
' containing the interface types.
TSecond.SetBaseTypeConstraint(baseType)
Dim interfaceTypes() As Type = {interfaceA, interfaceB}
TSecond.SetInterfaceConstraints(interfaceTypes)
' The following code adds a private field named ExampleField,
' of type TFirst.
Dim exField As FieldBuilder = _
myType.DefineField("ExampleField", TFirst, _
FieldAttributes.Private)
' Define a Shared method that takes an array of TFirst and
' returns a List(Of TFirst) containing all the elements of
' the array. To define this method it is necessary to create
' the type List(Of TFirst) by calling MakeGenericType on the
' generic type definition, List(Of T). (The T is omitted with
' the GetType operator when you get the generic type
' definition.) The parameter type is created by using the
' MakeArrayType method.
Dim listOf As Type = GetType(List(Of ))
Dim listOfTFirst As Type = listOf.MakeGenericType(TFirst)
Dim mParamTypes() As Type = {TFirst.MakeArrayType()}
Dim exMethod As MethodBuilder = _
myType.DefineMethod("ExampleMethod", _
MethodAttributes.Public Or MethodAttributes.Static, _
listOfTFirst, _
mParamTypes)
' Emit the method body.
' The method body consists of just three opcodes, to load
' the input array onto the execution stack, to call the
' List(Of TFirst) constructor that takes IEnumerable(Of TFirst),
' which does all the work of putting the input elements into
' the list, and to return, leaving the list on the stack. The
' hard work is getting the constructor.
' The GetConstructor method is not supported on a
' GenericTypeParameterBuilder, so it is not possible to get
' the constructor of List(Of TFirst) directly. There are two
' steps, first getting the constructor of List(Of T) and then
' calling a method that converts it to the corresponding
' constructor of List(Of TFirst).
' The constructor needed here is the one that takes an
' IEnumerable(Of T). Note, however, that this is not the
' generic type definition of IEnumerable(Of T); instead, the
' T from List(Of T) must be substituted for the T of
' IEnumerable(Of T). (This seems confusing only because both
' types have type parameters named T. That is why this example
' uses the somewhat silly names TFirst and TSecond.) To get
' the type of the constructor argument, take the generic
' type definition IEnumerable(Of T) (expressed as
' IEnumerable(Of ) when you use the GetType operator) and
' call MakeGenericType with the first generic type parameter
' of List(Of T). The constructor argument list must be passed
' as an array, with just one argument in this case.
' Now it is possible to get the constructor of List(Of T),
' using GetConstructor on the generic type definition. To get
' the constructor of List(Of TFirst), pass List(Of TFirst) and
' the constructor from List(Of T) to the static
' TypeBuilder.GetConstructor method.
Dim ilgen As ILGenerator = exMethod.GetILGenerator()
Dim ienumOf As Type = GetType(IEnumerable(Of ))
Dim listOfTParams() As Type = listOf.GetGenericArguments()
Dim TfromListOf As Type = listOfTParams(0)
Dim ienumOfT As Type = ienumOf.MakeGenericType(TfromListOf)
Dim ctorArgs() As Type = {ienumOfT}
Dim ctorPrep As ConstructorInfo = _
listOf.GetConstructor(ctorArgs)
Dim ctor As ConstructorInfo = _
TypeBuilder.GetConstructor(listOfTFirst, ctorPrep)
ilgen.Emit(OpCodes.Ldarg_0)
ilgen.Emit(OpCodes.Newobj, ctor)
ilgen.Emit(OpCodes.Ret)
' Create the type and save the assembly.
Dim finished As Type = myType.CreateType()
myAssembly.Save(myAsmName.Name & ".dll")
' Invoke the method.
' ExampleMethod is not generic, but the type it belongs to is
' generic, so in order to get a MethodInfo that can be invoked
' it is necessary to create a constructed type. The Example
' class satisfies the constraints on TFirst, because it is a
' reference type and has a default constructor. In order to
' have a class that satisfies the constraints on TSecond,
' this code example defines the ExampleDerived type. These
' two types are passed to MakeGenericMethod to create the
' constructed type.
Dim typeArgs() As Type = _
{GetType(Example), GetType(ExampleDerived)}
Dim constructed As Type = finished.MakeGenericType(typeArgs)
Dim mi As MethodInfo = constructed.GetMethod("ExampleMethod")
' Create an array of Example objects, as input to the generic
' method. This array must be passed as the only element of an
' array of arguments. The first argument of Invoke is
' Nothing, because ExampleMethod is Shared. Display the count
' on the resulting List(Of Example).
Dim input() As Example = {New Example(), New Example()}
Dim arguments() As Object = {input}
Dim listX As List(Of Example) = mi.Invoke(Nothing, arguments)
Console.WriteLine(vbLf & _
"There are {0} elements in the List(Of Example).", _
listX.Count _
DisplayGenericParameters(finished)
End Sub
Private Shared Sub DisplayGenericParameters(ByVal t As Type)
If Not t.IsGenericType Then
Console.WriteLine("Type '{0}' is not generic.")
Return
End If
If Not t.IsGenericTypeDefinition Then _
t = t.GetGenericTypeDefinition()
Dim typeParameters() As Type = t.GetGenericArguments()
Console.WriteLine(vbCrLf & _
"Listing {0} type parameters for type '{1}'.", _
typeParameters.Length, t)
For Each tParam As Type In typeParameters
Console.WriteLine(vbCrLf & "Type parameter {0}:", _
tParam.ToString())
For Each c As Type In tParam.GetGenericParameterConstraints()
If c.IsInterface Then
Console.WriteLine(" Interface constraint: {0}", c)
Console.WriteLine(" Base type constraint: {0}", c)
End If
ListConstraintAttributes(tParam)
Next tParam
End Sub
' List the constraint flags. The GenericParameterAttributes
' enumeration contains two sets of attributes, variance and
' constraints. For this example, only constraints are used.
Private Shared Sub ListConstraintAttributes(ByVal t As Type)
' Mask off the constraint flags.
Dim constraints As GenericParameterAttributes = _
t.GenericParameterAttributes And _
GenericParameterAttributes.SpecialConstraintMask
If (constraints And GenericParameterAttributes.ReferenceTypeConstraint) _
<> GenericParameterAttributes.None Then _
Console.WriteLine(" ReferenceTypeConstraint")
If (constraints And GenericParameterAttributes.NotNullableValueTypeConstraint) _
<> GenericParameterAttributes.None Then _
Console.WriteLine(" NotNullableValueTypeConstraint")
If (constraints And GenericParameterAttributes.DefaultConstructorConstraint) _
<> GenericParameterAttributes.None Then _
Console.WriteLine(" DefaultConstructorConstraint")
End Sub
End Class
' This code example produces the following output:
'Type 'Sample' is generic: False
'Type 'Sample' is generic: True
'There are 2 elements in the List(Of Example).
'Listing 2 type parameters for type 'Sample[TFirst,TSecond]'.
'Type parameter TFirst:
' ReferenceTypeConstraint
' DefaultConstructorConstraint
'Type parameter TSecond:
' Interface constraint: IExampleA
' Interface constraint: IExampleB
' Base type constraint: ExampleBase
GenericTypeParameterBuilder
使用反射发出
反射发出动态程序集应用场景
即将推出:在整个 2024 年,我们将逐步取消以“GitHub 问题”作为内容的反馈机制,并将其替换为新的反馈系统。 有关详细信息,请参阅:https://aka.ms/ContentUserFeedback。
提交和查看相关反馈