TypeBuilder 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
在运行时定义并创建类的新实例。
public ref class TypeBuilder sealed : Type
public ref class TypeBuilder sealed : System::Reflection::TypeInfo
public ref class TypeBuilder abstract : System::Reflection::TypeInfo
public ref class TypeBuilder sealed : Type, System::Runtime::InteropServices::_TypeBuilder
public ref class TypeBuilder sealed : System::Reflection::TypeInfo, System::Runtime::InteropServices::_TypeBuilder
public sealed class TypeBuilder : Type
public sealed class TypeBuilder : System.Reflection.TypeInfo
public abstract class TypeBuilder : System.Reflection.TypeInfo
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
public sealed class TypeBuilder : Type, System.Runtime.InteropServices._TypeBuilder
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class TypeBuilder : Type, System.Runtime.InteropServices._TypeBuilder
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class TypeBuilder : System.Reflection.TypeInfo, System.Runtime.InteropServices._TypeBuilder
type TypeBuilder = class
inherit Type
type TypeBuilder = class
inherit TypeInfo
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)>]
type TypeBuilder = class
inherit Type
interface _TypeBuilder
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type TypeBuilder = class
inherit Type
interface _TypeBuilder
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type TypeBuilder = class
inherit TypeInfo
interface _TypeBuilder
Public NotInheritable Class TypeBuilder
Inherits Type
Public NotInheritable Class TypeBuilder
Inherits TypeInfo
Public MustInherit Class TypeBuilder
Inherits TypeInfo
Public NotInheritable Class TypeBuilder
Inherits Type
Implements _TypeBuilder
Public NotInheritable Class TypeBuilder
Inherits TypeInfo
Implements _TypeBuilder
- 继承
- 继承
- 继承
- 属性
- 实现
示例
下面的代码示例演示如何定义和使用动态程序集。 示例程序集包含一种类型, MyDynamicType
具有私有字段、获取和设置私有字段的属性、初始化私有字段的构造函数,以及一个将用户提供的数字乘以私有字段值并返回结果的方法。
using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
void main()
{
// This code creates an assembly that contains one type,
// named "MyDynamicType", that has a private field, a property
// that gets and sets the private field, constructors that
// initialize the private field, and a method that multiplies
// a user-supplied number by the private field value and returns
// the result. In Visual C++ the type might look like this:
/*
public ref class MyDynamicType
{
private:
int m_number;
public:
MyDynamicType() : m_number(42) {};
MyDynamicType(int initNumber) : m_number(initNumber) {};
property int Number
{
int get() { return m_number; }
void set(int value) { m_number = value; }
}
int MyMethod(int multiplier)
{
return m_number * multiplier;
}
};
*/
AssemblyName^ aName = gcnew AssemblyName("DynamicAssemblyExample");
AssemblyBuilder^ ab =
AssemblyBuilder::DefineDynamicAssembly(
aName,
AssemblyBuilderAccess::Run);
// The module name is usually the same as the assembly name
ModuleBuilder^ mb =
ab->DefineDynamicModule(aName->Name);
TypeBuilder^ tb = mb->DefineType(
"MyDynamicType",
TypeAttributes::Public);
// Add a private field of type int (Int32).
FieldBuilder^ fbNumber = tb->DefineField(
"m_number",
int::typeid,
FieldAttributes::Private);
// Define a constructor that takes an integer argument and
// stores it in the private field.
array<Type^>^ parameterTypes = { int::typeid };
ConstructorBuilder^ ctor1 = tb->DefineConstructor(
MethodAttributes::Public,
CallingConventions::Standard,
parameterTypes);
ILGenerator^ ctor1IL = ctor1->GetILGenerator();
// For a constructor, argument zero is a reference to the new
// instance. Push it on the stack before calling the base
// class constructor. Specify the default constructor of the
// base class (System::Object) by passing an empty array of
// types (Type::EmptyTypes) to GetConstructor.
ctor1IL->Emit(OpCodes::Ldarg_0);
ctor1IL->Emit(OpCodes::Call,
Object::typeid->GetConstructor(Type::EmptyTypes));
// Push the instance on the stack before pushing the argument
// that is to be assigned to the private field m_number.
ctor1IL->Emit(OpCodes::Ldarg_0);
ctor1IL->Emit(OpCodes::Ldarg_1);
ctor1IL->Emit(OpCodes::Stfld, fbNumber);
ctor1IL->Emit(OpCodes::Ret);
// Define a default constructor that supplies a default value
// for the private field. For parameter types, pass the empty
// array of types or pass nullptr.
ConstructorBuilder^ ctor0 = tb->DefineConstructor(
MethodAttributes::Public,
CallingConventions::Standard,
Type::EmptyTypes);
ILGenerator^ ctor0IL = ctor0->GetILGenerator();
ctor0IL->Emit(OpCodes::Ldarg_0);
ctor0IL->Emit(OpCodes::Call,
Object::typeid->GetConstructor(Type::EmptyTypes));
// For a constructor, argument zero is a reference to the new
// instance. Push it on the stack before pushing the default
// value on the stack.
ctor0IL->Emit(OpCodes::Ldarg_0);
ctor0IL->Emit(OpCodes::Ldc_I4_S, 42);
ctor0IL->Emit(OpCodes::Stfld, fbNumber);
ctor0IL->Emit(OpCodes::Ret);
// Define a property named Number that gets and sets the private
// field.
//
// The last argument of DefineProperty is nullptr, because the
// property has no parameters. (If you don't specify nullptr, you must
// specify an array of Type objects. For a parameterless property,
// use the built-in array with no elements: Type::EmptyTypes)
PropertyBuilder^ pbNumber = tb->DefineProperty(
"Number",
PropertyAttributes::HasDefault,
int::typeid,
nullptr);
// The property "set" and property "get" methods require a special
// set of attributes.
MethodAttributes getSetAttr = MethodAttributes::Public |
MethodAttributes::SpecialName | MethodAttributes::HideBySig;
// Define the "get" accessor method for Number. The method returns
// an integer and has no arguments. (Note that nullptr could be
// used instead of Types::EmptyTypes)
MethodBuilder^ mbNumberGetAccessor = tb->DefineMethod(
"get_Number",
getSetAttr,
int::typeid,
Type::EmptyTypes);
ILGenerator^ numberGetIL = mbNumberGetAccessor->GetILGenerator();
// For an instance property, argument zero is the instance. Load the
// instance, then load the private field and return, leaving the
// field value on the stack.
numberGetIL->Emit(OpCodes::Ldarg_0);
numberGetIL->Emit(OpCodes::Ldfld, fbNumber);
numberGetIL->Emit(OpCodes::Ret);
// Define the "set" accessor method for Number, which has no return
// type and takes one argument of type int (Int32).
MethodBuilder^ mbNumberSetAccessor = tb->DefineMethod(
"set_Number",
getSetAttr,
nullptr,
gcnew array<Type^> { int::typeid });
ILGenerator^ numberSetIL = mbNumberSetAccessor->GetILGenerator();
// Load the instance and then the numeric argument, then store the
// argument in the field.
numberSetIL->Emit(OpCodes::Ldarg_0);
numberSetIL->Emit(OpCodes::Ldarg_1);
numberSetIL->Emit(OpCodes::Stfld, fbNumber);
numberSetIL->Emit(OpCodes::Ret);
// Last, map the "get" and "set" accessor methods to the
// PropertyBuilder. The property is now complete.
pbNumber->SetGetMethod(mbNumberGetAccessor);
pbNumber->SetSetMethod(mbNumberSetAccessor);
// Define a method that accepts an integer argument and returns
// the product of that integer and the private field m_number. This
// time, the array of parameter types is created on the fly.
MethodBuilder^ meth = tb->DefineMethod(
"MyMethod",
MethodAttributes::Public,
int::typeid,
gcnew array<Type^> { int::typeid });
ILGenerator^ methIL = meth->GetILGenerator();
// To retrieve the private instance field, load the instance it
// belongs to (argument zero). After loading the field, load the
// argument one and then multiply. Return from the method with
// the return value (the product of the two numbers) on the
// execution stack.
methIL->Emit(OpCodes::Ldarg_0);
methIL->Emit(OpCodes::Ldfld, fbNumber);
methIL->Emit(OpCodes::Ldarg_1);
methIL->Emit(OpCodes::Mul);
methIL->Emit(OpCodes::Ret);
// Finish the type->
Type^ t = tb->CreateType();
// Because AssemblyBuilderAccess includes Run, the code can be
// executed immediately. Start by getting reflection objects for
// the method and the property.
MethodInfo^ mi = t->GetMethod("MyMethod");
PropertyInfo^ pi = t->GetProperty("Number");
// Create an instance of MyDynamicType using the default
// constructor.
Object^ o1 = Activator::CreateInstance(t);
// Display the value of the property, then change it to 127 and
// display it again. Use nullptr to indicate that the property
// has no index.
Console::WriteLine("o1->Number: {0}", pi->GetValue(o1, nullptr));
pi->SetValue(o1, 127, nullptr);
Console::WriteLine("o1->Number: {0}", pi->GetValue(o1, nullptr));
// Call MyMethod, passing 22, and display the return value, 22
// times 127. Arguments must be passed as an array, even when
// there is only one.
array<Object^>^ arguments = { 22 };
Console::WriteLine("o1->MyMethod(22): {0}",
mi->Invoke(o1, arguments));
// Create an instance of MyDynamicType using the constructor
// that specifies m_Number. The constructor is identified by
// matching the types in the argument array. In this case,
// the argument array is created on the fly. Display the
// property value.
Object^ o2 = Activator::CreateInstance(t,
gcnew array<Object^> { 5280 });
Console::WriteLine("o2->Number: {0}", pi->GetValue(o2, nullptr));
};
/* This code produces the following output:
o1->Number: 42
o1->Number: 127
o1->MyMethod(22): 2794
o2->Number: 5280
*/
using System;
using System.Reflection;
using System.Reflection.Emit;
class DemoAssemblyBuilder
{
public static void Main()
{
// This code creates an assembly that contains one type,
// named "MyDynamicType", that has a private field, a property
// that gets and sets the private field, constructors that
// initialize the private field, and a method that multiplies
// a user-supplied number by the private field value and returns
// the result. In C# the type might look like this:
/*
public class MyDynamicType
{
private int m_number;
public MyDynamicType() : this(42) {}
public MyDynamicType(int initNumber)
{
m_number = initNumber;
}
public int Number
{
get { return m_number; }
set { m_number = value; }
}
public int MyMethod(int multiplier)
{
return m_number * multiplier;
}
}
*/
var aName = new AssemblyName("DynamicAssemblyExample");
AssemblyBuilder ab =
AssemblyBuilder.DefineDynamicAssembly(
aName,
AssemblyBuilderAccess.Run);
// The module name is usually the same as the assembly name.
ModuleBuilder mb = ab.DefineDynamicModule(aName.Name ?? "DynamicAssemblyExample");
TypeBuilder tb = mb.DefineType(
"MyDynamicType",
TypeAttributes.Public);
// Add a private field of type int (Int32).
FieldBuilder fbNumber = tb.DefineField(
"m_number",
typeof(int),
FieldAttributes.Private);
// Define a constructor that takes an integer argument and
// stores it in the private field.
Type[] parameterTypes = { typeof(int) };
ConstructorBuilder ctor1 = tb.DefineConstructor(
MethodAttributes.Public,
CallingConventions.Standard,
parameterTypes);
ILGenerator ctor1IL = ctor1.GetILGenerator();
// For a constructor, argument zero is a reference to the new
// instance. Push it on the stack before calling the base
// class constructor. Specify the default constructor of the
// base class (System.Object) by passing an empty array of
// types (Type.EmptyTypes) to GetConstructor.
ctor1IL.Emit(OpCodes.Ldarg_0);
ConstructorInfo? ci = typeof(object).GetConstructor(Type.EmptyTypes);
ctor1IL.Emit(OpCodes.Call, ci!);
// Push the instance on the stack before pushing the argument
// that is to be assigned to the private field m_number.
ctor1IL.Emit(OpCodes.Ldarg_0);
ctor1IL.Emit(OpCodes.Ldarg_1);
ctor1IL.Emit(OpCodes.Stfld, fbNumber);
ctor1IL.Emit(OpCodes.Ret);
// Define a default constructor that supplies a default value
// for the private field. For parameter types, pass the empty
// array of types or pass null.
ConstructorBuilder ctor0 = tb.DefineConstructor(
MethodAttributes.Public,
CallingConventions.Standard,
Type.EmptyTypes);
ILGenerator ctor0IL = ctor0.GetILGenerator();
// For a constructor, argument zero is a reference to the new
// instance. Push it on the stack before pushing the default
// value on the stack, then call constructor ctor1.
ctor0IL.Emit(OpCodes.Ldarg_0);
ctor0IL.Emit(OpCodes.Ldc_I4_S, 42);
ctor0IL.Emit(OpCodes.Call, ctor1);
ctor0IL.Emit(OpCodes.Ret);
// Define a property named Number that gets and sets the private
// field.
//
// The last argument of DefineProperty is null, because the
// property has no parameters. (If you don't specify null, you must
// specify an array of Type objects. For a parameterless property,
// use the built-in array with no elements: Type.EmptyTypes)
PropertyBuilder pbNumber = tb.DefineProperty(
"Number",
PropertyAttributes.HasDefault,
typeof(int),
null);
// The property "set" and property "get" methods require a special
// set of attributes.
MethodAttributes getSetAttr = MethodAttributes.Public |
MethodAttributes.SpecialName | MethodAttributes.HideBySig;
// Define the "get" accessor method for Number. The method returns
// an integer and has no arguments. (Note that null could be
// used instead of Types.EmptyTypes)
MethodBuilder mbNumberGetAccessor = tb.DefineMethod(
"get_Number",
getSetAttr,
typeof(int),
Type.EmptyTypes);
ILGenerator numberGetIL = mbNumberGetAccessor.GetILGenerator();
// For an instance property, argument zero is the instance. Load the
// instance, then load the private field and return, leaving the
// field value on the stack.
numberGetIL.Emit(OpCodes.Ldarg_0);
numberGetIL.Emit(OpCodes.Ldfld, fbNumber);
numberGetIL.Emit(OpCodes.Ret);
// Define the "set" accessor method for Number, which has no return
// type and takes one argument of type int (Int32).
MethodBuilder mbNumberSetAccessor = tb.DefineMethod(
"set_Number",
getSetAttr,
null,
new Type[] { typeof(int) });
ILGenerator numberSetIL = mbNumberSetAccessor.GetILGenerator();
// Load the instance and then the numeric argument, then store the
// argument in the field.
numberSetIL.Emit(OpCodes.Ldarg_0);
numberSetIL.Emit(OpCodes.Ldarg_1);
numberSetIL.Emit(OpCodes.Stfld, fbNumber);
numberSetIL.Emit(OpCodes.Ret);
// Last, map the "get" and "set" accessor methods to the
// PropertyBuilder. The property is now complete.
pbNumber.SetGetMethod(mbNumberGetAccessor);
pbNumber.SetSetMethod(mbNumberSetAccessor);
// Define a method that accepts an integer argument and returns
// the product of that integer and the private field m_number. This
// time, the array of parameter types is created on the fly.
MethodBuilder meth = tb.DefineMethod(
"MyMethod",
MethodAttributes.Public,
typeof(int),
new Type[] { typeof(int) });
ILGenerator methIL = meth.GetILGenerator();
// To retrieve the private instance field, load the instance it
// belongs to (argument zero). After loading the field, load the
// argument one and then multiply. Return from the method with
// the return value (the product of the two numbers) on the
// execution stack.
methIL.Emit(OpCodes.Ldarg_0);
methIL.Emit(OpCodes.Ldfld, fbNumber);
methIL.Emit(OpCodes.Ldarg_1);
methIL.Emit(OpCodes.Mul);
methIL.Emit(OpCodes.Ret);
// Finish the type.
Type? t = tb.CreateType();
// Because AssemblyBuilderAccess includes Run, the code can be
// executed immediately. Start by getting reflection objects for
// the method and the property.
MethodInfo? mi = t?.GetMethod("MyMethod");
PropertyInfo? pi = t?.GetProperty("Number");
// Create an instance of MyDynamicType using the default
// constructor.
object? o1 = null;
if (t is not null)
o1 = Activator.CreateInstance(t);
// Display the value of the property, then change it to 127 and
// display it again. Use null to indicate that the property
// has no index.
Console.WriteLine("o1.Number: {0}", pi?.GetValue(o1, null));
pi?.SetValue(o1, 127, null);
Console.WriteLine("o1.Number: {0}", pi?.GetValue(o1, null));
// Call MyMethod, passing 22, and display the return value, 22
// times 127. Arguments must be passed as an array, even when
// there is only one.
object[] arguments = { 22 };
Console.WriteLine("o1.MyMethod(22): {0}",
mi?.Invoke(o1, arguments));
// Create an instance of MyDynamicType using the constructor
// that specifies m_Number. The constructor is identified by
// matching the types in the argument array. In this case,
// the argument array is created on the fly. Display the
// property value.
object? o2 = null;
if (t is not null)
o2 = Activator.CreateInstance(t, new object[] { 5280 });
Console.WriteLine("o2.Number: {0}", pi?.GetValue(o2, null));
}
}
/* This code produces the following output:
o1.Number: 42
o1.Number: 127
o1.MyMethod(22): 2794
o2.Number: 5280
*/
Imports System.Reflection
Imports System.Reflection.Emit
Class DemoAssemblyBuilder
Public Shared Sub Main()
' This code creates an assembly that contains one type,
' named "MyDynamicType", that has a private field, a property
' that gets and sets the private field, constructors that
' initialize the private field, and a method that multiplies
' a user-supplied number by the private field value and returns
' the result. The code might look like this in Visual Basic:
'
'Public Class MyDynamicType
' Private m_number As Integer
'
' Public Sub New()
' Me.New(42)
' End Sub
'
' Public Sub New(ByVal initNumber As Integer)
' m_number = initNumber
' End Sub
'
' Public Property Number As Integer
' Get
' Return m_number
' End Get
' Set
' m_Number = Value
' End Set
' End Property
'
' Public Function MyMethod(ByVal multiplier As Integer) As Integer
' Return m_Number * multiplier
' End Function
'End Class
Dim aName As New AssemblyName("DynamicAssemblyExample")
Dim ab As AssemblyBuilder = _
AssemblyBuilder.DefineDynamicAssembly( _
aName, _
AssemblyBuilderAccess.Run)
' The module name is usually the same as the assembly name.
Dim mb As ModuleBuilder = ab.DefineDynamicModule( _
aName.Name)
Dim tb As TypeBuilder = _
mb.DefineType("MyDynamicType", TypeAttributes.Public)
' Add a private field of type Integer (Int32).
Dim fbNumber As FieldBuilder = tb.DefineField( _
"m_number", _
GetType(Integer), _
FieldAttributes.Private)
' Define a constructor that takes an integer argument and
' stores it in the private field.
Dim parameterTypes() As Type = { GetType(Integer) }
Dim ctor1 As ConstructorBuilder = _
tb.DefineConstructor( _
MethodAttributes.Public, _
CallingConventions.Standard, _
parameterTypes)
Dim ctor1IL As ILGenerator = ctor1.GetILGenerator()
' For a constructor, argument zero is a reference to the new
' instance. Push it on the stack before calling the base
' class constructor. Specify the default constructor of the
' base class (System.Object) by passing an empty array of
' types (Type.EmptyTypes) to GetConstructor.
ctor1IL.Emit(OpCodes.Ldarg_0)
ctor1IL.Emit(OpCodes.Call, _
GetType(Object).GetConstructor(Type.EmptyTypes))
' Push the instance on the stack before pushing the argument
' that is to be assigned to the private field m_number.
ctor1IL.Emit(OpCodes.Ldarg_0)
ctor1IL.Emit(OpCodes.Ldarg_1)
ctor1IL.Emit(OpCodes.Stfld, fbNumber)
ctor1IL.Emit(OpCodes.Ret)
' Define a default constructor that supplies a default value
' for the private field. For parameter types, pass the empty
' array of types or pass Nothing.
Dim ctor0 As ConstructorBuilder = tb.DefineConstructor( _
MethodAttributes.Public, _
CallingConventions.Standard, _
Type.EmptyTypes)
Dim ctor0IL As ILGenerator = ctor0.GetILGenerator()
' For a constructor, argument zero is a reference to the new
' instance. Push it on the stack before pushing the default
' value on the stack, then call constructor ctor1.
ctor0IL.Emit(OpCodes.Ldarg_0)
ctor0IL.Emit(OpCodes.Ldc_I4_S, 42)
ctor0IL.Emit(OpCodes.Call, ctor1)
ctor0IL.Emit(OpCodes.Ret)
' Define a property named Number that gets and sets the private
' field.
'
' The last argument of DefineProperty is Nothing, because the
' property has no parameters. (If you don't specify Nothing, you must
' specify an array of Type objects. For a parameterless property,
' use the built-in array with no elements: Type.EmptyTypes)
Dim pbNumber As PropertyBuilder = tb.DefineProperty( _
"Number", _
PropertyAttributes.HasDefault, _
GetType(Integer), _
Nothing)
' The property Set and property Get methods require a special
' set of attributes.
Dim getSetAttr As MethodAttributes = _
MethodAttributes.Public Or MethodAttributes.SpecialName _
Or MethodAttributes.HideBySig
' Define the "get" accessor method for Number. The method returns
' an integer and has no arguments. (Note that Nothing could be
' used instead of Types.EmptyTypes)
Dim mbNumberGetAccessor As MethodBuilder = tb.DefineMethod( _
"get_Number", _
getSetAttr, _
GetType(Integer), _
Type.EmptyTypes)
Dim numberGetIL As ILGenerator = mbNumberGetAccessor.GetILGenerator()
' For an instance property, argument zero is the instance. Load the
' instance, then load the private field and return, leaving the
' field value on the stack.
numberGetIL.Emit(OpCodes.Ldarg_0)
numberGetIL.Emit(OpCodes.Ldfld, fbNumber)
numberGetIL.Emit(OpCodes.Ret)
' Define the "set" accessor method for Number, which has no return
' type and takes one argument of type Integer (Int32).
Dim mbNumberSetAccessor As MethodBuilder = _
tb.DefineMethod( _
"set_Number", _
getSetAttr, _
Nothing, _
New Type() { GetType(Integer) })
Dim numberSetIL As ILGenerator = mbNumberSetAccessor.GetILGenerator()
' Load the instance and then the numeric argument, then store the
' argument in the field.
numberSetIL.Emit(OpCodes.Ldarg_0)
numberSetIL.Emit(OpCodes.Ldarg_1)
numberSetIL.Emit(OpCodes.Stfld, fbNumber)
numberSetIL.Emit(OpCodes.Ret)
' Last, map the "get" and "set" accessor methods to the
' PropertyBuilder. The property is now complete.
pbNumber.SetGetMethod(mbNumberGetAccessor)
pbNumber.SetSetMethod(mbNumberSetAccessor)
' Define a method that accepts an integer argument and returns
' the product of that integer and the private field m_number. This
' time, the array of parameter types is created on the fly.
Dim meth As MethodBuilder = tb.DefineMethod( _
"MyMethod", _
MethodAttributes.Public, _
GetType(Integer), _
New Type() { GetType(Integer) })
Dim methIL As ILGenerator = meth.GetILGenerator()
' To retrieve the private instance field, load the instance it
' belongs to (argument zero). After loading the field, load the
' argument one and then multiply. Return from the method with
' the return value (the product of the two numbers) on the
' execution stack.
methIL.Emit(OpCodes.Ldarg_0)
methIL.Emit(OpCodes.Ldfld, fbNumber)
methIL.Emit(OpCodes.Ldarg_1)
methIL.Emit(OpCodes.Mul)
methIL.Emit(OpCodes.Ret)
' Finish the type.
Dim t As Type = tb.CreateType()
' Because AssemblyBuilderAccess includes Run, the code can be
' executed immediately. Start by getting reflection objects for
' the method and the property.
Dim mi As MethodInfo = t.GetMethod("MyMethod")
Dim pi As PropertyInfo = t.GetProperty("Number")
' Create an instance of MyDynamicType using the default
' constructor.
Dim o1 As Object = Activator.CreateInstance(t)
' Display the value of the property, then change it to 127 and
' display it again. Use Nothing to indicate that the property
' has no index.
Console.WriteLine("o1.Number: {0}", pi.GetValue(o1, Nothing))
pi.SetValue(o1, 127, Nothing)
Console.WriteLine("o1.Number: {0}", pi.GetValue(o1, Nothing))
' Call MyMethod, passing 22, and display the return value, 22
' times 127. Arguments must be passed as an array, even when
' there is only one.
Dim arguments() As Object = { 22 }
Console.WriteLine("o1.MyMethod(22): {0}", _
mi.Invoke(o1, arguments))
' Create an instance of MyDynamicType using the constructor
' that specifies m_Number. The constructor is identified by
' matching the types in the argument array. In this case,
' the argument array is created on the fly. Display the
' property value.
Dim o2 As Object = Activator.CreateInstance(t, _
New Object() { 5280 })
Console.WriteLine("o2.Number: {0}", pi.GetValue(o2, Nothing))
End Sub
End Class
' This code produces the following output:
'
'o1.Number: 42
'o1.Number: 127
'o1.MyMethod(22): 2794
'o2.Number: 5280
以下代码示例演示如何使用 TypeBuilder
动态生成类型。
using namespace System;
using namespace System::Threading;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
Type^ DynamicDotProductGen()
{
Type^ ivType = nullptr;
array<Type^>^temp0 = {int::typeid,int::typeid,int::typeid};
array<Type^>^ctorParams = temp0;
AppDomain^ myDomain = Thread::GetDomain();
AssemblyName^ myAsmName = gcnew AssemblyName;
myAsmName->Name = "IntVectorAsm";
AssemblyBuilder^ myAsmBuilder = myDomain->DefineDynamicAssembly( myAsmName, AssemblyBuilderAccess::RunAndSave );
ModuleBuilder^ IntVectorModule = myAsmBuilder->DefineDynamicModule( "IntVectorModule", "Vector.dll" );
TypeBuilder^ ivTypeBld = IntVectorModule->DefineType( "IntVector", TypeAttributes::Public );
FieldBuilder^ xField = ivTypeBld->DefineField( "x", int::typeid, FieldAttributes::Private );
FieldBuilder^ yField = ivTypeBld->DefineField( "y", int::typeid, FieldAttributes::Private );
FieldBuilder^ zField = ivTypeBld->DefineField( "z", int::typeid, FieldAttributes::Private );
Type^ objType = Type::GetType( "System.Object" );
ConstructorInfo^ objCtor = objType->GetConstructor( gcnew array<Type^>(0) );
ConstructorBuilder^ ivCtor = ivTypeBld->DefineConstructor( MethodAttributes::Public, CallingConventions::Standard, ctorParams );
ILGenerator^ ctorIL = ivCtor->GetILGenerator();
ctorIL->Emit( OpCodes::Ldarg_0 );
ctorIL->Emit( OpCodes::Call, objCtor );
ctorIL->Emit( OpCodes::Ldarg_0 );
ctorIL->Emit( OpCodes::Ldarg_1 );
ctorIL->Emit( OpCodes::Stfld, xField );
ctorIL->Emit( OpCodes::Ldarg_0 );
ctorIL->Emit( OpCodes::Ldarg_2 );
ctorIL->Emit( OpCodes::Stfld, yField );
ctorIL->Emit( OpCodes::Ldarg_0 );
ctorIL->Emit( OpCodes::Ldarg_3 );
ctorIL->Emit( OpCodes::Stfld, zField );
ctorIL->Emit( OpCodes::Ret );
// This method will find the dot product of the stored vector
// with another.
array<Type^>^temp1 = {ivTypeBld};
array<Type^>^dpParams = temp1;
// Here, you create a MethodBuilder containing the
// name, the attributes (public, static, private, and so on),
// the return type (int, in this case), and a array of Type
// indicating the type of each parameter. Since the sole parameter
// is a IntVector, the very class you're creating, you will
// pass in the TypeBuilder (which is derived from Type) instead of
// a Type object for IntVector, avoiding an exception.
// -- This method would be declared in C# as:
// public int DotProduct(IntVector aVector)
MethodBuilder^ dotProductMthd = ivTypeBld->DefineMethod( "DotProduct", MethodAttributes::Public, int::typeid, dpParams );
// A ILGenerator can now be spawned, attached to the MethodBuilder.
ILGenerator^ mthdIL = dotProductMthd->GetILGenerator();
// Here's the body of our function, in MSIL form. We're going to find the
// "dot product" of the current vector instance with the passed vector
// instance. For reference purposes, the equation is:
// (x1 * x2) + (y1 * y2) + (z1 * z2) = the dot product
// First, you'll load the reference to the current instance "this"
// stored in argument 0 (ldarg.0) onto the stack. Ldfld, the subsequent
// instruction, will pop the reference off the stack and look up the
// field "x", specified by the FieldInfo token "xField".
mthdIL->Emit( OpCodes::Ldarg_0 );
mthdIL->Emit( OpCodes::Ldfld, xField );
// That completed, the value stored at field "x" is now atop the stack.
// Now, you'll do the same for the Object reference we passed as a
// parameter, stored in argument 1 (ldarg.1). After Ldfld executed,
// you'll have the value stored in field "x" for the passed instance
// atop the stack.
mthdIL->Emit( OpCodes::Ldarg_1 );
mthdIL->Emit( OpCodes::Ldfld, xField );
// There will now be two values atop the stack - the "x" value for the
// current vector instance, and the "x" value for the passed instance.
// You'll now multiply them, and push the result onto the evaluation stack.
mthdIL->Emit( OpCodes::Mul_Ovf_Un );
// Now, repeat this for the "y" fields of both vectors.
mthdIL->Emit( OpCodes::Ldarg_0 );
mthdIL->Emit( OpCodes::Ldfld, yField );
mthdIL->Emit( OpCodes::Ldarg_1 );
mthdIL->Emit( OpCodes::Ldfld, yField );
mthdIL->Emit( OpCodes::Mul_Ovf_Un );
// At this time, the results of both multiplications should be atop
// the stack. You'll now add them and push the result onto the stack.
mthdIL->Emit( OpCodes::Add_Ovf_Un );
// Multiply both "z" field and push the result onto the stack.
mthdIL->Emit( OpCodes::Ldarg_0 );
mthdIL->Emit( OpCodes::Ldfld, zField );
mthdIL->Emit( OpCodes::Ldarg_1 );
mthdIL->Emit( OpCodes::Ldfld, zField );
mthdIL->Emit( OpCodes::Mul_Ovf_Un );
// Finally, add the result of multiplying the "z" fields with the
// result of the earlier addition, and push the result - the dot product -
// onto the stack.
mthdIL->Emit( OpCodes::Add_Ovf_Un );
// The "ret" opcode will pop the last value from the stack and return it
// to the calling method. You're all done!
mthdIL->Emit( OpCodes::Ret );
ivType = ivTypeBld->CreateType();
return ivType;
}
int main()
{
Type^ IVType = nullptr;
Object^ aVector1 = nullptr;
Object^ aVector2 = nullptr;
array<Type^>^temp2 = {int::typeid,int::typeid,int::typeid};
array<Type^>^aVtypes = temp2;
array<Object^>^temp3 = {10,10,10};
array<Object^>^aVargs1 = temp3;
array<Object^>^temp4 = {20,20,20};
array<Object^>^aVargs2 = temp4;
// Call the method to build our dynamic class.
IVType = DynamicDotProductGen();
Console::WriteLine( "---" );
ConstructorInfo^ myDTctor = IVType->GetConstructor( aVtypes );
aVector1 = myDTctor->Invoke( aVargs1 );
aVector2 = myDTctor->Invoke( aVargs2 );
array<Object^>^passMe = gcnew array<Object^>(1);
passMe[ 0 ] = dynamic_cast<Object^>(aVector2);
Console::WriteLine( "(10, 10, 10) . (20, 20, 20) = {0}", IVType->InvokeMember( "DotProduct", BindingFlags::InvokeMethod, nullptr, aVector1, passMe ) );
}
// +++ OUTPUT +++
// ---
// (10, 10, 10) . (20, 20, 20) = 600
using System;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;
class TestILGenerator
{
public static Type DynamicDotProductGen()
{
Type ivType = null;
Type[] ctorParams = new Type[] { typeof(int),
typeof(int),
typeof(int)};
AppDomain myDomain = Thread.GetDomain();
AssemblyName myAsmName = new AssemblyName();
myAsmName.Name = "IntVectorAsm";
AssemblyBuilder myAsmBuilder = myDomain.DefineDynamicAssembly(
myAsmName,
AssemblyBuilderAccess.RunAndSave);
ModuleBuilder IntVectorModule = myAsmBuilder.DefineDynamicModule("IntVectorModule",
"Vector.dll");
TypeBuilder ivTypeBld = IntVectorModule.DefineType("IntVector",
TypeAttributes.Public);
FieldBuilder xField = ivTypeBld.DefineField("x", typeof(int),
FieldAttributes.Private);
FieldBuilder yField = ivTypeBld.DefineField("y", typeof(int),
FieldAttributes.Private);
FieldBuilder zField = ivTypeBld.DefineField("z", typeof(int),
FieldAttributes.Private);
Type objType = Type.GetType("System.Object");
ConstructorInfo objCtor = objType.GetConstructor(new Type[0]);
ConstructorBuilder ivCtor = ivTypeBld.DefineConstructor(
MethodAttributes.Public,
CallingConventions.Standard,
ctorParams);
ILGenerator ctorIL = ivCtor.GetILGenerator();
ctorIL.Emit(OpCodes.Ldarg_0);
ctorIL.Emit(OpCodes.Call, objCtor);
ctorIL.Emit(OpCodes.Ldarg_0);
ctorIL.Emit(OpCodes.Ldarg_1);
ctorIL.Emit(OpCodes.Stfld, xField);
ctorIL.Emit(OpCodes.Ldarg_0);
ctorIL.Emit(OpCodes.Ldarg_2);
ctorIL.Emit(OpCodes.Stfld, yField);
ctorIL.Emit(OpCodes.Ldarg_0);
ctorIL.Emit(OpCodes.Ldarg_3);
ctorIL.Emit(OpCodes.Stfld, zField);
ctorIL.Emit(OpCodes.Ret);
// This method will find the dot product of the stored vector
// with another.
Type[] dpParams = new Type[] { ivTypeBld };
// Here, you create a MethodBuilder containing the
// name, the attributes (public, static, private, and so on),
// the return type (int, in this case), and a array of Type
// indicating the type of each parameter. Since the sole parameter
// is a IntVector, the very class you're creating, you will
// pass in the TypeBuilder (which is derived from Type) instead of
// a Type object for IntVector, avoiding an exception.
// -- This method would be declared in C# as:
// public int DotProduct(IntVector aVector)
MethodBuilder dotProductMthd = ivTypeBld.DefineMethod(
"DotProduct",
MethodAttributes.Public,
typeof(int),
dpParams);
// A ILGenerator can now be spawned, attached to the MethodBuilder.
ILGenerator mthdIL = dotProductMthd.GetILGenerator();
// Here's the body of our function, in MSIL form. We're going to find the
// "dot product" of the current vector instance with the passed vector
// instance. For reference purposes, the equation is:
// (x1 * x2) + (y1 * y2) + (z1 * z2) = the dot product
// First, you'll load the reference to the current instance "this"
// stored in argument 0 (ldarg.0) onto the stack. Ldfld, the subsequent
// instruction, will pop the reference off the stack and look up the
// field "x", specified by the FieldInfo token "xField".
mthdIL.Emit(OpCodes.Ldarg_0);
mthdIL.Emit(OpCodes.Ldfld, xField);
// That completed, the value stored at field "x" is now atop the stack.
// Now, you'll do the same for the object reference we passed as a
// parameter, stored in argument 1 (ldarg.1). After Ldfld executed,
// you'll have the value stored in field "x" for the passed instance
// atop the stack.
mthdIL.Emit(OpCodes.Ldarg_1);
mthdIL.Emit(OpCodes.Ldfld, xField);
// There will now be two values atop the stack - the "x" value for the
// current vector instance, and the "x" value for the passed instance.
// You'll now multiply them, and push the result onto the evaluation stack.
mthdIL.Emit(OpCodes.Mul_Ovf_Un);
// Now, repeat this for the "y" fields of both vectors.
mthdIL.Emit(OpCodes.Ldarg_0);
mthdIL.Emit(OpCodes.Ldfld, yField);
mthdIL.Emit(OpCodes.Ldarg_1);
mthdIL.Emit(OpCodes.Ldfld, yField);
mthdIL.Emit(OpCodes.Mul_Ovf_Un);
// At this time, the results of both multiplications should be atop
// the stack. You'll now add them and push the result onto the stack.
mthdIL.Emit(OpCodes.Add_Ovf_Un);
// Multiply both "z" field and push the result onto the stack.
mthdIL.Emit(OpCodes.Ldarg_0);
mthdIL.Emit(OpCodes.Ldfld, zField);
mthdIL.Emit(OpCodes.Ldarg_1);
mthdIL.Emit(OpCodes.Ldfld, zField);
mthdIL.Emit(OpCodes.Mul_Ovf_Un);
// Finally, add the result of multiplying the "z" fields with the
// result of the earlier addition, and push the result - the dot product -
// onto the stack.
mthdIL.Emit(OpCodes.Add_Ovf_Un);
// The "ret" opcode will pop the last value from the stack and return it
// to the calling method. You're all done!
mthdIL.Emit(OpCodes.Ret);
ivType = ivTypeBld.CreateType();
return ivType;
}
public static void Main() {
Type IVType = null;
object aVector1 = null;
object aVector2 = null;
Type[] aVtypes = new Type[] {typeof(int), typeof(int), typeof(int)};
object[] aVargs1 = new object[] {10, 10, 10};
object[] aVargs2 = new object[] {20, 20, 20};
// Call the method to build our dynamic class.
IVType = DynamicDotProductGen();
Console.WriteLine("---");
ConstructorInfo myDTctor = IVType.GetConstructor(aVtypes);
aVector1 = myDTctor.Invoke(aVargs1);
aVector2 = myDTctor.Invoke(aVargs2);
object[] passMe = new object[1];
passMe[0] = (object)aVector2;
Console.WriteLine("(10, 10, 10) . (20, 20, 20) = {0}",
IVType.InvokeMember("DotProduct",
BindingFlags.InvokeMethod,
null,
aVector1,
passMe));
// +++ OUTPUT +++
// ---
// (10, 10, 10) . (20, 20, 20) = 600
}
}
Imports System.Threading
Imports System.Reflection
Imports System.Reflection.Emit
_
Class TestILGenerator
Public Shared Function DynamicDotProductGen() As Type
Dim ivType As Type = Nothing
Dim ctorParams() As Type = {GetType(Integer), GetType(Integer), GetType(Integer)}
Dim myDomain As AppDomain = Thread.GetDomain()
Dim myAsmName As New AssemblyName()
myAsmName.Name = "IntVectorAsm"
Dim myAsmBuilder As AssemblyBuilder = myDomain.DefineDynamicAssembly( _
myAsmName, _
AssemblyBuilderAccess.RunAndSave)
Dim IntVectorModule As ModuleBuilder = myAsmBuilder.DefineDynamicModule( _
"IntVectorModule", _
"Vector.dll")
Dim ivTypeBld As TypeBuilder = IntVectorModule.DefineType("IntVector", TypeAttributes.Public)
Dim xField As FieldBuilder = ivTypeBld.DefineField("x", _
GetType(Integer), _
FieldAttributes.Private)
Dim yField As FieldBuilder = ivTypeBld.DefineField("y", _
GetType(Integer), _
FieldAttributes.Private)
Dim zField As FieldBuilder = ivTypeBld.DefineField("z", _
GetType(Integer), _
FieldAttributes.Private)
Dim objType As Type = Type.GetType("System.Object")
Dim objCtor As ConstructorInfo = objType.GetConstructor(New Type() {})
Dim ivCtor As ConstructorBuilder = ivTypeBld.DefineConstructor( _
MethodAttributes.Public, _
CallingConventions.Standard, _
ctorParams)
Dim ctorIL As ILGenerator = ivCtor.GetILGenerator()
ctorIL.Emit(OpCodes.Ldarg_0)
ctorIL.Emit(OpCodes.Call, objCtor)
ctorIL.Emit(OpCodes.Ldarg_0)
ctorIL.Emit(OpCodes.Ldarg_1)
ctorIL.Emit(OpCodes.Stfld, xField)
ctorIL.Emit(OpCodes.Ldarg_0)
ctorIL.Emit(OpCodes.Ldarg_2)
ctorIL.Emit(OpCodes.Stfld, yField)
ctorIL.Emit(OpCodes.Ldarg_0)
ctorIL.Emit(OpCodes.Ldarg_3)
ctorIL.Emit(OpCodes.Stfld, zField)
ctorIL.Emit(OpCodes.Ret)
' Now, you'll construct the method find the dot product of two vectors. First,
' let's define the parameters that will be accepted by the method. In this case,
' it's an IntVector itself!
Dim dpParams() As Type = {ivTypeBld}
' Here, you create a MethodBuilder containing the
' name, the attributes (public, static, private, and so on),
' the return type (int, in this case), and a array of Type
' indicating the type of each parameter. Since the sole parameter
' is a IntVector, the very class you're creating, you will
' pass in the TypeBuilder (which is derived from Type) instead of
' a Type object for IntVector, avoiding an exception.
' -- This method would be declared in VB.NET as:
' Public Function DotProduct(IntVector aVector) As Integer
Dim dotProductMthd As MethodBuilder = ivTypeBld.DefineMethod("DotProduct", _
MethodAttributes.Public, GetType(Integer), _
dpParams)
' A ILGenerator can now be spawned, attached to the MethodBuilder.
Dim mthdIL As ILGenerator = dotProductMthd.GetILGenerator()
' Here's the body of our function, in MSIL form. We're going to find the
' "dot product" of the current vector instance with the passed vector
' instance. For reference purposes, the equation is:
' (x1 * x2) + (y1 * y2) + (z1 * z2) = the dot product
' First, you'll load the reference to the current instance "this"
' stored in argument 0 (ldarg.0) onto the stack. Ldfld, the subsequent
' instruction, will pop the reference off the stack and look up the
' field "x", specified by the FieldInfo token "xField".
mthdIL.Emit(OpCodes.Ldarg_0)
mthdIL.Emit(OpCodes.Ldfld, xField)
' That completed, the value stored at field "x" is now atop the stack.
' Now, you'll do the same for the object reference we passed as a
' parameter, stored in argument 1 (ldarg.1). After Ldfld executed,
' you'll have the value stored in field "x" for the passed instance
' atop the stack.
mthdIL.Emit(OpCodes.Ldarg_1)
mthdIL.Emit(OpCodes.Ldfld, xField)
' There will now be two values atop the stack - the "x" value for the
' current vector instance, and the "x" value for the passed instance.
' You'll now multiply them, and push the result onto the evaluation stack.
mthdIL.Emit(OpCodes.Mul_Ovf_Un)
' Now, repeat this for the "y" fields of both vectors.
mthdIL.Emit(OpCodes.Ldarg_0)
mthdIL.Emit(OpCodes.Ldfld, yField)
mthdIL.Emit(OpCodes.Ldarg_1)
mthdIL.Emit(OpCodes.Ldfld, yField)
mthdIL.Emit(OpCodes.Mul_Ovf_Un)
' At this time, the results of both multiplications should be atop
' the stack. You'll now add them and push the result onto the stack.
mthdIL.Emit(OpCodes.Add_Ovf_Un)
' Multiply both "z" field and push the result onto the stack.
mthdIL.Emit(OpCodes.Ldarg_0)
mthdIL.Emit(OpCodes.Ldfld, zField)
mthdIL.Emit(OpCodes.Ldarg_1)
mthdIL.Emit(OpCodes.Ldfld, zField)
mthdIL.Emit(OpCodes.Mul_Ovf_Un)
' Finally, add the result of multiplying the "z" fields with the
' result of the earlier addition, and push the result - the dot product -
' onto the stack.
mthdIL.Emit(OpCodes.Add_Ovf_Un)
' The "ret" opcode will pop the last value from the stack and return it
' to the calling method. You're all done!
mthdIL.Emit(OpCodes.Ret)
ivType = ivTypeBld.CreateType()
Return ivType
End Function 'DynamicDotProductGen
Public Shared Sub Main()
Dim IVType As Type = Nothing
Dim aVector1 As Object = Nothing
Dim aVector2 As Object = Nothing
Dim aVtypes() As Type = {GetType(Integer), GetType(Integer), GetType(Integer)}
Dim aVargs1() As Object = {10, 10, 10}
Dim aVargs2() As Object = {20, 20, 20}
' Call the method to build our dynamic class.
IVType = DynamicDotProductGen()
Dim myDTctor As ConstructorInfo = IVType.GetConstructor(aVtypes)
aVector1 = myDTctor.Invoke(aVargs1)
aVector2 = myDTctor.Invoke(aVargs2)
Console.WriteLine("---")
Dim passMe(0) As Object
passMe(0) = CType(aVector2, Object)
Console.WriteLine("(10, 10, 10) . (20, 20, 20) = {0}", _
IVType.InvokeMember("DotProduct", BindingFlags.InvokeMethod, _
Nothing, aVector1, passMe))
End Sub
End Class
' +++ OUTPUT +++
' ---
' (10, 10, 10) . (20, 20, 20) = 600
注解
有关此 API 的详细信息,请参阅 TypeBuilder 的补充 API 说明。
构造函数
TypeBuilder() |
初始化 TypeBuilder 类的新实例。 |
字段
UnspecifiedTypeSize |
表示未指定类型的总大小。 |
属性
Assembly |
检索包含此类型定义的动态程序集。 |
AssemblyQualifiedName |
返回由程序集的显示名称限定的此类型的全名。 |
Attributes |
在运行时定义并创建类的新实例。 |
Attributes |
获取与 Type 关联的属性。 (继承自 Type) |
Attributes |
在运行时定义并创建类的新实例。 (继承自 TypeInfo) |
BaseType |
检索此类型的基类型。 |
ContainsGenericParameters |
在运行时定义并创建类的新实例。 |
ContainsGenericParameters |
获取一个值,该值指示当前 Type 对象是否具有尚未被特定类型替代的类型参数。 (继承自 Type) |
ContainsGenericParameters |
在运行时定义并创建类的新实例。 (继承自 TypeInfo) |
CustomAttributes |
获取包含此成员自定义属性的集合。 (继承自 MemberInfo) |
DeclaredConstructors |
获取由当前类型声明的构造函数的集合。 (继承自 TypeInfo) |
DeclaredEvents |
获取由当前类型定义的事件的集合。 (继承自 TypeInfo) |
DeclaredFields |
获取由当前类型定义的字段的集合。 (继承自 TypeInfo) |
DeclaredMembers |
获取由当前类型定义的成员的集合。 (继承自 TypeInfo) |
DeclaredMethods |
获取由当前类型定义的方法的集合。 (继承自 TypeInfo) |
DeclaredNestedTypes |
获取由当前类型定义的嵌套类型的集合。 (继承自 TypeInfo) |
DeclaredProperties |
获取由当前类型定义的属性的集合。 (继承自 TypeInfo) |
DeclaringMethod |
获取声明了当前泛型类型参数的方法。 |
DeclaringMethod |
获取一个表示声明方法的 MethodBase(如果当前 Type 表示泛型方法的一个类型参数)。 (继承自 Type) |
DeclaringType |
返回声明此类型的类型。 |
FullName |
检索此类型的完整路径。 |
GenericParameterAttributes |
获取一个值,该值指示当前泛型类型参数的协变和特殊约束。 |
GenericParameterAttributes |
获取描述当前泛型类型参数的协变和特殊约束的 GenericParameterAttributes 标志。 (继承自 Type) |
GenericParameterPosition |
获取声明参数的泛型类型的类型参数列表中的类型参数位置。 |
GenericParameterPosition |
当 Type 对象表示泛型类型或泛型方法的类型参数时,获取类型参数在声明它的泛型类型或方法的类型参数列表中的位置。 (继承自 Type) |
GenericTypeArguments |
在运行时定义并创建类的新实例。 |
GenericTypeArguments |
获取此类型泛型类型参数的数组。 (继承自 Type) |
GenericTypeArguments |
在运行时定义并创建类的新实例。 (继承自 TypeInfo) |
GenericTypeParameters |
获取当前实例泛型类型参数的数组。 (继承自 TypeInfo) |
GUID |
检索此类型的 GUID。 |
HasElementType |
获取一个值,通过该值指示当前 Type 是包含还是引用另一类型,即当前 Type 是数组、指针还是通过引用传递。 (继承自 Type) |
HasElementType |
在运行时定义并创建类的新实例。 (继承自 TypeInfo) |
ImplementedInterfaces |
获取当前类型实现的接口的集合。 (继承自 TypeInfo) |
IsAbstract |
获取一个值,通过该值指示 Type 是否为抽象的并且必须被重写。 (继承自 Type) |
IsAbstract |
在运行时定义并创建类的新实例。 (继承自 TypeInfo) |
IsAnsiClass |
获取一个值,该值指示是否为 |
IsAnsiClass |
在运行时定义并创建类的新实例。 (继承自 TypeInfo) |
IsArray |
获取一个值,该值指示类型是否为数组。 (继承自 Type) |
IsArray |
在运行时定义并创建类的新实例。 (继承自 TypeInfo) |
IsAutoClass |
获取一个值,该值指示是否为 |
IsAutoClass |
在运行时定义并创建类的新实例。 (继承自 TypeInfo) |
IsAutoLayout |
获取指示当前类型的字段是否由公共语言运行时自动放置的值。 (继承自 Type) |
IsAutoLayout |
在运行时定义并创建类的新实例。 (继承自 TypeInfo) |
IsByRef |
获取一个值,该值指示 Type 是否由引用传递。 (继承自 Type) |
IsByRef |
在运行时定义并创建类的新实例。 (继承自 TypeInfo) |
IsByRefLike |
获取一个值,该值指示类型是否是与 byref 类似的结构。 |
IsByRefLike |
获取一个值,该值指示类型是否是与 byref 类似的结构。 (继承自 Type) |
IsClass |
获取一个值,通过该值指示 Type 是否是一个类或委托;即,不是值类型或接口。 (继承自 Type) |
IsClass |
在运行时定义并创建类的新实例。 (继承自 TypeInfo) |
IsCollectible |
获取一个值,该值指示此 MemberInfo 对象是否是包含在可回收的 AssemblyLoadContext 中的程序集的一部分。 (继承自 MemberInfo) |
IsCOMObject |
获取一个值,通过该值指示 Type 是否为 COM 对象。 (继承自 Type) |
IsCOMObject |
在运行时定义并创建类的新实例。 (继承自 TypeInfo) |
IsConstructedGenericType |
获取指示此对象是否表示构造的泛型类型的值。 |
IsConstructedGenericType |
获取指示此对象是否表示构造的泛型类型的值。 你可以创建构造型泛型类型的实例。 (继承自 Type) |
IsContextful |
获取一个值,通过该值指示 Type 在上下文中是否可以被承载。 (继承自 Type) |
IsEnum |
在运行时定义并创建类的新实例。 |
IsEnum |
获取一个值,该值指示当前的 Type 是否表示枚举。 (继承自 Type) |
IsEnum |
在运行时定义并创建类的新实例。 (继承自 TypeInfo) |
IsExplicitLayout |
获取指示当前类型的字段是否放置在显式指定的偏移量处的值。 (继承自 Type) |
IsExplicitLayout |
在运行时定义并创建类的新实例。 (继承自 TypeInfo) |
IsFunctionPointer |
获取一个值,该值指示当前 Type 是否为函数指针。 (继承自 Type) |
IsGenericMethodParameter |
获取一个值,该值指示当前 Type 是否表示泛型方法定义中的类型参数。 (继承自 Type) |
IsGenericParameter |
获取一个值,该值指示当前类型是否是泛型类型参数。 |
IsGenericParameter |
获取一个值,该值指示当前 Type 是否表示泛型类型或方法的定义中的类型参数。 (继承自 Type) |
IsGenericType |
获取一个值,该值指示当前类型是否是泛型类型。 |
IsGenericType |
获取一个值,该值指示当前类型是否是泛型类型。 (继承自 Type) |
IsGenericTypeDefinition |
获取一个值,该值指示当前 TypeBuilder 是否表示可以用来构造其他泛型类型的泛型类型定义。 |
IsGenericTypeDefinition |
获取一个值,该值指示当前 Type 是否表示可以用来构造其他泛型类型的泛型类型定义。 (继承自 Type) |
IsGenericTypeParameter |
获取一个值,该值指示当前 Type 是否表示泛型类型定义中的类型参数。 (继承自 Type) |
IsImport |
获取一个值,该值指示 Type 是否应用了 ComImportAttribute 属性,如果应用了该属性,则表示它是从 COM 类型库导入的。 (继承自 Type) |
IsImport |
在运行时定义并创建类的新实例。 (继承自 TypeInfo) |
IsInterface |
获取一个值,通过该值指示 Type 是否是一个接口;即,不是类或值类型。 (继承自 Type) |
IsInterface |
在运行时定义并创建类的新实例。 (继承自 TypeInfo) |
IsLayoutSequential |
获取指示当前类型的字段是否按顺序(定义顺序或发送到元数据的顺序)放置的值。 (继承自 Type) |
IsLayoutSequential |
在运行时定义并创建类的新实例。 (继承自 TypeInfo) |
IsMarshalByRef |
获取一个值,该值指示 Type 是否按引用进行封送。 (继承自 Type) |
IsMarshalByRef |
在运行时定义并创建类的新实例。 (继承自 TypeInfo) |
IsNested |
获取一个指示当前 Type 对象是否表示其定义嵌套在另一个类型的定义之内的类型的值。 (继承自 Type) |
IsNested |
在运行时定义并创建类的新实例。 (继承自 TypeInfo) |
IsNestedAssembly |
获取一个值,通过该值指示 Type 是否是嵌套的并且只能在它自己的程序集内可见。 (继承自 Type) |
IsNestedAssembly |
在运行时定义并创建类的新实例。 (继承自 TypeInfo) |
IsNestedFamANDAssem |
获取一个值,通过该值指示 Type 是否是嵌套的并且只对同时属于自己家族和自己程序集的类可见。 (继承自 Type) |
IsNestedFamANDAssem |
在运行时定义并创建类的新实例。 (继承自 TypeInfo) |
IsNestedFamily |
获取一个值,通过该值指示 Type 是否是嵌套的并且只能在它自己的家族内可见。 (继承自 Type) |
IsNestedFamily |
在运行时定义并创建类的新实例。 (继承自 TypeInfo) |
IsNestedFamORAssem |
获取一个值,通过该值指示 Type 是否是嵌套的并且只对属于它自己的家族或属于它自己的程序集的类可见。 (继承自 Type) |
IsNestedFamORAssem |
在运行时定义并创建类的新实例。 (继承自 TypeInfo) |
IsNestedPrivate |
获取一个值,通过该值指示 Type 是否是嵌套的并声明为私有。 (继承自 Type) |
IsNestedPrivate |
在运行时定义并创建类的新实例。 (继承自 TypeInfo) |
IsNestedPublic |
获取一个值,通过该值指示类是否是嵌套的并且声明为公共的。 (继承自 Type) |
IsNestedPublic |
在运行时定义并创建类的新实例。 (继承自 TypeInfo) |
IsNotPublic |
获取一个值,该值指示 Type 是否声明为公共类型。 (继承自 Type) |
IsNotPublic |
在运行时定义并创建类的新实例。 (继承自 TypeInfo) |
IsPointer |
获取一个值,通过该值指示 Type 是否为指针。 (继承自 Type) |
IsPointer |
在运行时定义并创建类的新实例。 (继承自 TypeInfo) |
IsPrimitive |
获取一个值,通过该值指示 Type 是否为基元类型之一。 (继承自 Type) |
IsPrimitive |
在运行时定义并创建类的新实例。 (继承自 TypeInfo) |
IsPublic |
获取一个值,该值指示 Type 是否声明为公共类型。 (继承自 Type) |
IsPublic |
在运行时定义并创建类的新实例。 (继承自 TypeInfo) |
IsSealed |
获取一个值,该值指示 Type 是否声明为密封的。 (继承自 Type) |
IsSealed |
在运行时定义并创建类的新实例。 (继承自 TypeInfo) |
IsSecurityCritical |
获取一个值,该值指示当前类型是安全-关键的还是安全-可靠-关键的,且因此是否可执行关键操作。 |
IsSecurityCritical |
获取一个值,该值指示当前的类型在当前信任级别上是安全关键的还是安全可靠关键的,并因此可以执行关键操作。 (继承自 Type) |
IsSecuritySafeCritical |
获取一个值,该值指示当前类型是否为安全-可靠-关键,即它是否可执行关键操作且可由透明代码访问。 |
IsSecuritySafeCritical |
获取一个值,该值指示当前类型在当前信任级别上是否是安全可靠关键的;即它是否可以执行关键操作并可以由透明代码访问。 (继承自 Type) |
IsSecurityTransparent |
获取一个值,该值指示当前类型是否透明,且因此是否无法指定关键操作。 |
IsSecurityTransparent |
获取一个值,该值指示当前类型在当前信任级别上是否是透明的而无法执行关键操作。 (继承自 Type) |
IsSerializable |
在运行时定义并创建类的新实例。 |
IsSerializable |
已过时.
获取一个值, Type 该值指示 是否可序列化二进制。 (继承自 Type) |
IsSerializable |
在运行时定义并创建类的新实例。 (继承自 TypeInfo) |
IsSignatureType |
获取一个值,该值指示类型是否是签名类型。 (继承自 Type) |
IsSpecialName |
获取一个值,该值指示该类型是否具有需要特殊处理的名称。 (继承自 Type) |
IsSpecialName |
在运行时定义并创建类的新实例。 (继承自 TypeInfo) |
IsSZArray |
在运行时定义并创建类的新实例。 |
IsSZArray |
获取一个值,该值指示类型是否是仅可表示下限为零的一维数组的数组类型。 (继承自 Type) |
IsTypeDefinition |
在运行时定义并创建类的新实例。 |
IsTypeDefinition |
获取一个值,该值指示类型是否是类型定义。 (继承自 Type) |
IsUnicodeClass |
获取一个值,该值指示是否为 |
IsUnicodeClass |
在运行时定义并创建类的新实例。 (继承自 TypeInfo) |
IsUnmanagedFunctionPointer |
获取一个值,该值指示当前 Type 是否为非托管函数指针。 (继承自 Type) |
IsValueType |
获取一个值,通过该值指示 Type 是否为值类型。 (继承自 Type) |
IsValueType |
在运行时定义并创建类的新实例。 (继承自 TypeInfo) |
IsVariableBoundArray |
在运行时定义并创建类的新实例。 |
IsVariableBoundArray |
获取一个值,该值指示类型是否是可表示多维数组或具有任意下限的数组的数组类型。 (继承自 Type) |
IsVisible |
获取一个指示 Type 是否可由程序集之外的代码访问的值。 (继承自 Type) |
IsVisible |
在运行时定义并创建类的新实例。 (继承自 TypeInfo) |
MemberType |
获取一个指示此成员是类型还是嵌套类型的 MemberTypes 值。 (继承自 Type) |
MemberType |
在运行时定义并创建类的新实例。 (继承自 TypeInfo) |
MetadataToken |
获取一个标记,该标记用于标识元数据中的当前动态模块。 |
MetadataToken |
获取一个值,该值标识元数据元素。 (继承自 MemberInfo) |
Module |
检索包含此类型定义的动态模块。 |
Name |
检索此类型的名称。 |
Namespace |
检索定义了此 |
PackingSize |
检索此类型的包装大小。 |
PackingSizeCore |
在派生类中重写时,获取此类型的打包大小。 |
ReflectedType |
返回用于获取此类型的类型。 |
ReflectedType |
获取用于获取 |
Size |
检索类型的总大小。 |
SizeCore |
在派生类中重写时,获取类型的总大小。 |
StructLayoutAttribute |
获取一个描述当前类型的布局的 StructLayoutAttribute。 (继承自 Type) |
StructLayoutAttribute |
在运行时定义并创建类的新实例。 (继承自 TypeInfo) |
TypeHandle |
不支持动态模块。 |
TypeInitializer |
获取该类型的初始值设定项。 (继承自 Type) |
TypeInitializer |
在运行时定义并创建类的新实例。 (继承自 TypeInfo) |
TypeToken |
返回此类型的类型标记。 |
UnderlyingSystemType |
返回此 |
UnderlyingSystemType |
在运行时定义并创建类的新实例。 (继承自 TypeInfo) |