ConstructorBuilder 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
定義並表示動態類別的建構子。
public ref class ConstructorBuilder sealed : System::Reflection::ConstructorInfo, System::Runtime::InteropServices::_ConstructorBuilder
public ref class ConstructorBuilder sealed : System::Reflection::ConstructorInfo
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
public sealed class ConstructorBuilder : System.Reflection.ConstructorInfo, System.Runtime.InteropServices._ConstructorBuilder
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class ConstructorBuilder : System.Reflection.ConstructorInfo, System.Runtime.InteropServices._ConstructorBuilder
public sealed class ConstructorBuilder : System.Reflection.ConstructorInfo
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)>]
type ConstructorBuilder = class
inherit ConstructorInfo
interface _ConstructorBuilder
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type ConstructorBuilder = class
inherit ConstructorInfo
interface _ConstructorBuilder
type ConstructorBuilder = class
inherit ConstructorInfo
Public NotInheritable Class ConstructorBuilder
Inherits ConstructorInfo
Implements _ConstructorBuilder
Public NotInheritable Class ConstructorBuilder
Inherits ConstructorInfo
- 繼承
- 屬性
- 實作
範例
以下程式碼範例說明了 在情境 ConstructorBuilder下的 。
using System;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;
class TestCtorBuilder {
public static Type DynamicPointTypeGen() {
Type pointType = null;
Type[] ctorParams = new Type[] {typeof(int),
typeof(int),
typeof(int)};
AppDomain myDomain = Thread.GetDomain();
AssemblyName myAsmName = new AssemblyName();
myAsmName.Name = "MyDynamicAssembly";
AssemblyBuilder myAsmBuilder = myDomain.DefineDynamicAssembly(
myAsmName,
AssemblyBuilderAccess.RunAndSave);
ModuleBuilder pointModule = myAsmBuilder.DefineDynamicModule("PointModule",
"Point.dll");
TypeBuilder pointTypeBld = pointModule.DefineType("Point",
TypeAttributes.Public);
FieldBuilder xField = pointTypeBld.DefineField("x", typeof(int),
FieldAttributes.Public);
FieldBuilder yField = pointTypeBld.DefineField("y", typeof(int),
FieldAttributes.Public);
FieldBuilder zField = pointTypeBld.DefineField("z", typeof(int),
FieldAttributes.Public);
Type objType = Type.GetType("System.Object");
ConstructorInfo objCtor = objType.GetConstructor(new Type[0]);
ConstructorBuilder pointCtor = pointTypeBld.DefineConstructor(
MethodAttributes.Public,
CallingConventions.Standard,
ctorParams);
ILGenerator ctorIL = pointCtor.GetILGenerator();
// NOTE: ldarg.0 holds the "this" reference - ldarg.1, ldarg.2, and ldarg.3
// hold the actual passed parameters. ldarg.0 is used by instance methods
// to hold a reference to the current calling object instance. Static methods
// do not use arg.0, since they are not instantiated and hence no reference
// is needed to distinguish them.
ctorIL.Emit(OpCodes.Ldarg_0);
// Here, we wish to create an instance of System.Object by invoking its
// constructor, as specified above.
ctorIL.Emit(OpCodes.Call, objCtor);
// Now, we'll load the current instance ref in arg 0, along
// with the value of parameter "x" stored in arg 1, into stfld.
ctorIL.Emit(OpCodes.Ldarg_0);
ctorIL.Emit(OpCodes.Ldarg_1);
ctorIL.Emit(OpCodes.Stfld, xField);
// Now, we store arg 2 "y" in the current instance with stfld.
ctorIL.Emit(OpCodes.Ldarg_0);
ctorIL.Emit(OpCodes.Ldarg_2);
ctorIL.Emit(OpCodes.Stfld, yField);
// Last of all, arg 3 "z" gets stored in the current instance.
ctorIL.Emit(OpCodes.Ldarg_0);
ctorIL.Emit(OpCodes.Ldarg_3);
ctorIL.Emit(OpCodes.Stfld, zField);
// Our work complete, we return.
ctorIL.Emit(OpCodes.Ret);
// Now, let's create three very simple methods so we can see our fields.
string[] mthdNames = new string[] {"GetX", "GetY", "GetZ"};
foreach (string mthdName in mthdNames) {
MethodBuilder getFieldMthd = pointTypeBld.DefineMethod(
mthdName,
MethodAttributes.Public,
typeof(int),
null);
ILGenerator mthdIL = getFieldMthd.GetILGenerator();
mthdIL.Emit(OpCodes.Ldarg_0);
switch (mthdName) {
case "GetX": mthdIL.Emit(OpCodes.Ldfld, xField);
break;
case "GetY": mthdIL.Emit(OpCodes.Ldfld, yField);
break;
case "GetZ": mthdIL.Emit(OpCodes.Ldfld, zField);
break;
}
mthdIL.Emit(OpCodes.Ret);
}
// Finally, we create the type.
pointType = pointTypeBld.CreateType();
// Let's save it, just for posterity.
myAsmBuilder.Save("Point.dll");
return pointType;
}
public static void Main() {
Type myDynamicType = null;
object aPoint = null;
Type[] aPtypes = new Type[] {typeof(int), typeof(int), typeof(int)};
object[] aPargs = new object[] {4, 5, 6};
// Call the method to build our dynamic class.
myDynamicType = DynamicPointTypeGen();
Console.WriteLine("Some information about my new Type '{0}':",
myDynamicType.FullName);
Console.WriteLine("Assembly: '{0}'", myDynamicType.Assembly);
Console.WriteLine("Attributes: '{0}'", myDynamicType.Attributes);
Console.WriteLine("Module: '{0}'", myDynamicType.Module);
Console.WriteLine("Members: ");
foreach (MemberInfo member in myDynamicType.GetMembers()) {
Console.WriteLine("-- {0} {1};", member.MemberType, member.Name);
}
Console.WriteLine("---");
// Let's take a look at the constructor we created.
ConstructorInfo myDTctor = myDynamicType.GetConstructor(aPtypes);
Console.WriteLine("Constructor: {0};", myDTctor.ToString());
Console.WriteLine("---");
// Now, we get to use our dynamically-created class by invoking the constructor.
aPoint = myDTctor.Invoke(aPargs);
Console.WriteLine("aPoint is type {0}.", aPoint.GetType());
// Finally, let's reflect on the instance of our new type - aPoint - and
// make sure everything proceeded according to plan.
Console.WriteLine("aPoint.x = {0}",
myDynamicType.InvokeMember("GetX",
BindingFlags.InvokeMethod,
null,
aPoint,
new object[0]));
Console.WriteLine("aPoint.y = {0}",
myDynamicType.InvokeMember("GetY",
BindingFlags.InvokeMethod,
null,
aPoint,
new object[0]));
Console.WriteLine("aPoint.z = {0}",
myDynamicType.InvokeMember("GetZ",
BindingFlags.InvokeMethod,
null,
aPoint,
new object[0]));
// +++ OUTPUT +++
// Some information about my new Type 'Point':
// Assembly: 'MyDynamicAssembly, Version=0.0.0.0'
// Attributes: 'AutoLayout, AnsiClass, NotPublic, Public'
// Module: 'PointModule'
// Members:
// -- Field x;
// -- Field y;
// -- Field z;
// -- Method GetHashCode;
// -- Method Equals;
// -- Method ToString;
// -- Method GetType;
// -- Constructor .ctor;
// ---
// Constructor: Void .ctor(Int32, Int32, Int32);
// ---
// aPoint is type Point.
// aPoint.x = 4
// aPoint.y = 5
// aPoint.z = 6
}
}
Imports System.Threading
Imports System.Reflection
Imports System.Reflection.Emit
_
Class TestCtorBuilder
Public Shared Function DynamicPointTypeGen() As Type
Dim pointType 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 = "MyDynamicAssembly"
Dim myAsmBuilder As AssemblyBuilder = myDomain.DefineDynamicAssembly(myAsmName, AssemblyBuilderAccess.RunAndSave)
Dim pointModule As ModuleBuilder = myAsmBuilder.DefineDynamicModule("PointModule", "Point.dll")
Dim pointTypeBld As TypeBuilder = pointModule.DefineType("Point", TypeAttributes.Public)
Dim xField As FieldBuilder = pointTypeBld.DefineField("x", GetType(Integer), FieldAttributes.Public)
Dim yField As FieldBuilder = pointTypeBld.DefineField("y", GetType(Integer), FieldAttributes.Public)
Dim zField As FieldBuilder = pointTypeBld.DefineField("z", GetType(Integer), FieldAttributes.Public)
Dim objType As Type = Type.GetType("System.Object")
Dim objCtor As ConstructorInfo = objType.GetConstructor(New Type() {})
Dim pointCtor As ConstructorBuilder = pointTypeBld.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, ctorParams)
Dim ctorIL As ILGenerator = pointCtor.GetILGenerator()
' NOTE: ldarg.0 holds the "this" reference - ldarg.1, ldarg.2, and ldarg.3
' hold the actual passed parameters. ldarg.0 is used by instance methods
' to hold a reference to the current calling object instance. Static methods
' do not use arg.0, since they are not instantiated and hence no reference
' is needed to distinguish them.
ctorIL.Emit(OpCodes.Ldarg_0)
' Here, we wish to create an instance of System.Object by invoking its
' constructor, as specified above.
ctorIL.Emit(OpCodes.Call, objCtor)
' Now, we'll load the current instance ref in arg 0, along
' with the value of parameter "x" stored in arg 1, into stfld.
ctorIL.Emit(OpCodes.Ldarg_0)
ctorIL.Emit(OpCodes.Ldarg_1)
ctorIL.Emit(OpCodes.Stfld, xField)
' Now, we store arg 2 "y" in the current instance with stfld.
ctorIL.Emit(OpCodes.Ldarg_0)
ctorIL.Emit(OpCodes.Ldarg_2)
ctorIL.Emit(OpCodes.Stfld, yField)
' Last of all, arg 3 "z" gets stored in the current instance.
ctorIL.Emit(OpCodes.Ldarg_0)
ctorIL.Emit(OpCodes.Ldarg_3)
ctorIL.Emit(OpCodes.Stfld, zField)
' Our work complete, we return.
ctorIL.Emit(OpCodes.Ret)
' Now, let's create three very simple methods so we can see our fields.
Dim mthdNames() As String = {"GetX", "GetY", "GetZ"}
Dim mthdName As String
For Each mthdName In mthdNames
Dim getFieldMthd As MethodBuilder = pointTypeBld.DefineMethod(mthdName, MethodAttributes.Public, GetType(Integer), Nothing)
Dim mthdIL As ILGenerator = getFieldMthd.GetILGenerator()
mthdIL.Emit(OpCodes.Ldarg_0)
Select Case mthdName
Case "GetX"
mthdIL.Emit(OpCodes.Ldfld, xField)
Case "GetY"
mthdIL.Emit(OpCodes.Ldfld, yField)
Case "GetZ"
mthdIL.Emit(OpCodes.Ldfld, zField)
End Select
mthdIL.Emit(OpCodes.Ret)
Next mthdName
' Finally, we create the type.
pointType = pointTypeBld.CreateType()
' Let's save it, just for posterity.
myAsmBuilder.Save("Point.dll")
Return pointType
End Function 'DynamicPointTypeGen
Public Shared Sub Main()
Dim myDynamicType As Type = Nothing
Dim aPoint As Object = Nothing
Dim aPtypes() As Type = {GetType(Integer), GetType(Integer), GetType(Integer)}
Dim aPargs() As Object = {4, 5, 6}
' Call the method to build our dynamic class.
myDynamicType = DynamicPointTypeGen()
Console.WriteLine("Some information about my new Type '{0}':", myDynamicType.FullName)
Console.WriteLine("Assembly: '{0}'", myDynamicType.Assembly)
Console.WriteLine("Attributes: '{0}'", myDynamicType.Attributes)
Console.WriteLine("Module: '{0}'", myDynamicType.Module)
Console.WriteLine("Members: ")
Dim member As MemberInfo
For Each member In myDynamicType.GetMembers()
Console.WriteLine("-- {0} {1};", member.MemberType, member.Name)
Next member
Console.WriteLine("---")
' Let's take a look at the constructor we created.
Dim myDTctor As ConstructorInfo = myDynamicType.GetConstructor(aPtypes)
Console.WriteLine("Constructor: {0};", myDTctor.ToString())
Console.WriteLine("---")
' Now, we get to use our dynamically-created class by invoking the constructor.
aPoint = myDTctor.Invoke(aPargs)
Console.WriteLine("aPoint is type {0}.", aPoint.GetType())
' Finally, let's reflect on the instance of our new type - aPoint - and
' make sure everything proceeded according to plan.
Console.WriteLine("aPoint.x = {0}", myDynamicType.InvokeMember("GetX", BindingFlags.InvokeMethod, Nothing, aPoint, New Object() {}))
Console.WriteLine("aPoint.y = {0}", myDynamicType.InvokeMember("GetY", BindingFlags.InvokeMethod, Nothing, aPoint, New Object() {}))
Console.WriteLine("aPoint.z = {0}", myDynamicType.InvokeMember("GetZ", BindingFlags.InvokeMethod, Nothing, aPoint, New Object() {}))
End Sub
End Class
' +++ OUTPUT +++
' Some information about my new Type 'Point':
' Assembly: 'MyDynamicAssembly, Version=0.0.0.0'
' Attributes: 'AutoLayout, AnsiClass, NotPublic, Public'
' Module: 'PointModule'
' Members:
' -- Field x;
' -- Field y;
' -- Field z;
' -- Method GetHashCode;
' -- Method Equals;
' -- Method ToString;
' -- Method GetType;
' -- Constructor .ctor;
' ---
' Constructor: Void .ctor(Int32, Int32, Int32);
' ---
' aPoint is type Point.
' aPoint.x = 4
' aPoint.y = 5
' aPoint.z = 6
備註
ConstructorBuilder 用於完整描述中介語言(MSIL)中的建構子Microsoft包括名稱、屬性、簽名及建構子主體。 它與 TypeBuilder 類別結合使用,在執行時建立類別。 呼叫 DefineConstructor 以獲得 的 ConstructorBuilder實例。
如果你沒有為動態型別定義建構子,系統會自動提供一個無參數建構子,並呼叫基底類別的無參數建構子。
如果你用來 ConstructorBuilder 定義動態型別的建構子,則不會提供無參數建構子。 除了你定義的建構子外,你還有以下選項可以提供無參數建構子:
如果你想要一個無參數建構子,直接呼叫基底類別的無參數建構子,可以用這個 TypeBuilder.DefineDefaultConstructor 方法建立一個(並可選擇限制存取)。 請勿提供此無參數建構器的實作。 如果你有,嘗試使用建構子時會拋出例外。 當呼叫該 TypeBuilder.CreateType 方法時,不會拋出任何例外。
如果你想要一個無參數建構器,能做的不只是呼叫基底類別的無參數建構器,或呼叫基底類別的另一個建構器,或完全不同的事情,你必須使用該 TypeBuilder.DefineConstructor 方法來建立一個 ConstructorBuilder,並提供你自己的實作。
屬性
| 名稱 | Description |
|---|---|
| Attributes |
取得這個建構子的屬性。 |
| CallingConvention |
會得到 CallingConventions 一個取決於宣告類型是否為通用型別的值。 |
| CallingConvention |
會得到一個值,表示此方法的呼叫慣例。 (繼承來源 MethodBase) |
| ContainsGenericParameters |
會取得一個值,表示通用方法是否包含未指派的通用型別參數。 (繼承來源 MethodBase) |
| CustomAttributes |
會獲得包含該成員自訂屬性的集合。 (繼承來源 MemberInfo) |
| DeclaringType |
會取得宣 Type 告該成員的物件參考。 |
| InitLocals |
取得或設定此建構子中的局部變數是否應為零初始化。 |
| IsAbstract |
會得到一個值,表示該方法是否為抽象。 (繼承來源 MethodBase) |
| IsAssembly |
獲得一個值,表示此方法或建構子的潛在可見性是否由 Assembly描述;也就是說,該方法或建構器最多對同一組裝中的其他型別可見,對組裝外的衍生型別則無法看到。 (繼承來源 MethodBase) |
| IsConstructedGenericMethod |
定義並表示動態類別的建構子。 (繼承來源 MethodBase) |
| IsConstructor |
會得到一個值,表示該方法是否為建構子。 (繼承來源 MethodBase) |
| IsFamily |
獲得一個值,表示此方法或建構子的可見性是否由 Family描述;也就是說,該方法或建構子僅在其類別及其衍生類別中可見。 (繼承來源 MethodBase) |
| IsFamilyAndAssembly |
會得到一個值,表示此方法或建構子的可見性是否由 FamANDAssem描述;也就是說,該方法或建構子可以被導出類別呼叫,但前提是它們位於同一組建構中。 (繼承來源 MethodBase) |
| IsFamilyOrAssembly |
會得到一個值,表示此方法或建構子的潛在可見性是否由 FamORAssem描述;也就是說,該方法或建構子可以被派生類別呼叫,無論它們所在的位置,或同一組合語言中的類別。 (繼承來源 MethodBase) |
| IsFinal |
得到一個值,表示此方法是否為 |
| IsGenericMethod |
會得到一個值,表示該方法是否為通用。 (繼承來源 MethodBase) |
| IsGenericMethodDefinition |
會得到一個值,表示該方法是否為一般方法定義。 (繼承來源 MethodBase) |
| IsHideBySig |
會得到一個值,表示導出類別中是否只有同類型且簽名完全相同的成員被隱藏。 (繼承來源 MethodBase) |
| IsPrivate |
會獲得一個值,表示該成員是否為私人。 (繼承來源 MethodBase) |
| IsPublic |
會有一個值來表示這是否是一個公開方法。 (繼承來源 MethodBase) |
| IsSecurityCritical |
會獲得一個值,表示目前的方法或建構子在當前信任層級下是安全關鍵還是安全關鍵,因此可以執行關鍵操作。 (繼承來源 MethodBase) |
| IsSecuritySafeCritical |
獲得一個值,表示目前的方法或建構子在當前信任層級是否為安全關鍵;也就是說,它是否能執行關鍵操作,且是否能被透明程式碼存取。 (繼承來源 MethodBase) |
| IsSecurityTransparent |
會獲得一個值,表示目前的方法或建構子在目前信任層級是否透明,因此無法執行關鍵操作。 (繼承來源 MethodBase) |
| IsSpecialName |
會得到一個值,表示此方法是否有特殊名稱。 (繼承來源 MethodBase) |
| IsStatic |
獲得一個值,表示該方法是否為 |
| IsVirtual |
獲得一個值,表示該方法是否為 |
| MemberType |
會得到 MemberTypes 一個值,表示該成員是建構子。 (繼承來源 ConstructorInfo) |
| MetadataToken |
會得到一個識別元資料元素的值。 (繼承來源 MemberInfo) |
| MethodHandle |
取得方法的內部握柄。 使用此代言人來存取底層的元資料代言人。 |
| MethodImplementationFlags |
取得 MethodImplAttributes 指定方法實作屬性的旗標。 (繼承來源 MethodBase) |
| Module |
得到定義此建構子的動態模組。 |
| Name |
取得此建構子的名稱。 |
| ReflectedType |
包含該 Type 物件來源的參考。 |
| ReturnType |
已淘汰.
取得 |
| Signature |
以字串形式取得場的簽名。 |
方法
明確介面實作
擴充方法
| 名稱 | Description |
|---|---|
| GetCustomAttribute(MemberInfo, Type, Boolean) |
擷取指定類型的自訂屬性,套用於指定成員,並可選擇性地檢查該成員的祖先。 |
| GetCustomAttribute(MemberInfo, Type) |
擷取指定型別的自訂屬性,套用到指定成員。 |
| GetCustomAttribute<T>(MemberInfo, Boolean) |
擷取指定類型的自訂屬性,套用於指定成員,並可選擇性地檢查該成員的祖先。 |
| GetCustomAttribute<T>(MemberInfo) |
擷取指定型別的自訂屬性,套用到指定成員。 |
| GetCustomAttributes(MemberInfo, Boolean) |
擷取一套套用於指定成員的自訂屬性,並可選擇性地檢查該成員的祖先。 |
| GetCustomAttributes(MemberInfo, Type, Boolean) |
擷取一組指定類型的自訂屬性,套用於指定成員,並可選擇性檢查該成員的祖先。 |
| GetCustomAttributes(MemberInfo, Type) |
擷取一組指定類型的自訂屬性,套用到指定成員。 |
| GetCustomAttributes(MemberInfo) |
擷取一套套用於指定成員的自訂屬性集合。 |
| GetCustomAttributes<T>(MemberInfo, Boolean) |
擷取一組指定類型的自訂屬性,套用於指定成員,並可選擇性檢查該成員的祖先。 |
| GetCustomAttributes<T>(MemberInfo) |
擷取一組指定類型的自訂屬性,套用到指定成員。 |
| IsDefined(MemberInfo, Type, Boolean) |
表示是否將特定類型的自訂屬性套用於指定成員,並可選擇套用於其祖先。 |
| IsDefined(MemberInfo, Type) |
表示是否套用特定類型的自訂屬性給指定成員。 |