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 |
取得表示目前類型的欄位是否已由 Common Language Runtime 自動配置版面的值。 (繼承來源 Type) |
IsAutoLayout |
在執行階段定義和建立類別的新執行個體。 (繼承來源 TypeInfo) |
IsByRef |
取得值,指出 Type 是否以傳址方式傳遞。 (繼承來源 Type) |
IsByRef |
在執行階段定義和建立類別的新執行個體。 (繼承來源 TypeInfo) |
IsByRefLike |
取得值,指出類型是否為 byref-like 結構。 |
IsByRefLike |
取得值,指出類型是否為 byref-like 結構。 (繼承來源 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 屬性 (Attribute),亦即其是否從 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 |
取得值,指出類別是否為巢狀 (Nest) 並且宣告為公用 (Public)。 (繼承來源 Type) |
IsNestedPublic |
在執行階段定義和建立類別的新執行個體。 (繼承來源 TypeInfo) |
IsNotPublic |
取得值,指出 Type 是否未宣告為公用。 (繼承來源 Type) |
IsNotPublic |
在執行階段定義和建立類別的新執行個體。 (繼承來源 TypeInfo) |
IsPointer |
取得值,指出 Type 是否為指標。 (繼承來源 Type) |
IsPointer |
在執行階段定義和建立類別的新執行個體。 (繼承來源 TypeInfo) |
IsPrimitive |
取得值,指出 Type 是否為其中一個基本類型 (Primitive 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 是否為 Unmanaged 函式指標。 (繼承來源 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) |