ConstructorInfo 类

发现类构造函数的属性 (Attribute) 并提供对构造函数元数据的访问权。

**命名空间:**System.Reflection
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
<ClassInterfaceAttribute(ClassInterfaceType.None)> _
Public MustInherit Class ConstructorInfo
    Inherits MethodBase
    Implements _ConstructorInfo
用法
Dim instance As ConstructorInfo
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
[ClassInterfaceAttribute(ClassInterfaceType.None)] 
public abstract class ConstructorInfo : MethodBase, _ConstructorInfo
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
[ClassInterfaceAttribute(ClassInterfaceType::None)] 
public ref class ConstructorInfo abstract : public MethodBase, _ConstructorInfo
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.None) */ 
public abstract class ConstructorInfo extends MethodBase implements _ConstructorInfo
SerializableAttribute 
ComVisibleAttribute(true) 
ClassInterfaceAttribute(ClassInterfaceType.None) 
public abstract class ConstructorInfo extends MethodBase implements _ConstructorInfo

备注

ConstructorInfo 用于发现构造函数的属性 (Attribute) 及调用构造函数。通过对 ConstructorInfo 调用 Invoke 来创建对象,其中 ConstructorInfo 是由 Type 对象的 GetConstructorsGetConstructor 方法返回的。

提示

ConstructorInfoMethodBase 继承了几个成员,如可用于检查泛型方法的 IsGenericMethod。在 .NET Framework 2.0 版中,构造函数不能是泛型的,因此这些成员返回 false 或 空引用(在 Visual Basic 中为 Nothing)。

给继承者的说明ConstructorInfo 继承时,必须重写以下成员重载:Invoke(BindingFlags,Binder,Object[],CultureInfo)

示例

下面的示例使用 GetConstructorBindingFlags 通过 ConstructorInfo 来查找与指定搜索条件相匹配的构造函数。

Public Class MyClass1
    Public Sub New(ByVal i As Integer)
    End Sub
    Public Shared Sub Main()
        Try
            Dim myType As Type = GetType(MyClass1)
            Dim types(0) As Type
            types(0) = GetType(Integer)
            ' Get the public instance constructor that takes an integer parameter.
            Dim constructorInfoObj As ConstructorInfo = _
                        myType.GetConstructor(BindingFlags.Instance Or _
                        BindingFlags.Public, Nothing, _
                        CallingConventions.HasThis, types, Nothing)
            If Not (constructorInfoObj Is Nothing) Then
                Console.WriteLine("The constructor of MyClass1 that " + _
                                  "is a public instance method and takes an " + _
                                  "integer as a parameter is: ")
                Console.WriteLine(constructorInfoObj.ToString())
            Else
                Console.WriteLine("The constructor MyClass1 that " + _
                                  "is a public instance method and takes an " + _
                                  "integer as a parameter is not available.")
            End If
        Catch e As ArgumentNullException
            Console.WriteLine("ArgumentNullException: " + e.Message)
        Catch e As ArgumentException
            Console.WriteLine("ArgumentException: " + e.Message)
        Catch e As SecurityException
            Console.WriteLine("SecurityException: " + e.Message)
        Catch e As Exception
            Console.WriteLine("Exception: " + e.Message)
        End Try
    End Sub
End Class
using System;
using System.Reflection;
using System.Security;

public class MyClass1
{
    public MyClass1(int i){}
    public static void Main()
    {
        try
        {
            Type  myType = typeof(MyClass1);
            Type[] types = new Type[1];
            types[0] = typeof(int);
            // Get the public instance constructor that takes an integer parameter.
            ConstructorInfo constructorInfoObj = myType.GetConstructor(
                BindingFlags.Instance | BindingFlags.Public, null,
                CallingConventions.HasThis, types, null);
            if(constructorInfoObj != null)
            {
                Console.WriteLine("The constructor of MyClass1 that is a public " +
                    "instance method and takes an integer as a parameter is: ");
                Console.WriteLine(constructorInfoObj.ToString());
            }
            else
            {
                Console.WriteLine("The constructor of MyClass1 that is a public instance " +
                    "method and takes an integer as a parameter is not available.");
            }
        }
        catch(ArgumentNullException e)
        {
            Console.WriteLine("ArgumentNullException: " + e.Message);
        }
        catch(ArgumentException e)
        {
            Console.WriteLine("ArgumentException: " + e.Message);
        }
        catch(SecurityException e)
        {
            Console.WriteLine("SecurityException: " + e.Message);
        }
        catch(Exception e)
        {
            Console.WriteLine("Exception: " + e.Message);
        }
    }
}
using namespace System;
using namespace System::Reflection;
using namespace System::Security;
public ref class MyClass1
{
public:
   MyClass1( int i ){}

};

int main()
{
   try
   {
      Type^ myType = MyClass1::typeid;
      array<Type^>^types = gcnew array<Type^>(1);
      types[ 0 ] = int::typeid;
      
      // Get the public instance constructor that takes an integer parameter.
      ConstructorInfo^ constructorInfoObj = myType->GetConstructor( static_cast<BindingFlags>(BindingFlags::Instance | BindingFlags::Public), nullptr, CallingConventions::HasThis, types, nullptr );
      if ( constructorInfoObj != nullptr )
      {
         Console::WriteLine( "The constructor of MyClass1 that is a public instance method and takes an integer as a parameter is: " );
         Console::WriteLine( constructorInfoObj );
      }
      else
      {
         Console::WriteLine( "The constructor of MyClass1 that is a public instance method and takes an integer as a parameter is not available." );
      }
   }
   catch ( ArgumentNullException^ e ) 
   {
      Console::WriteLine( "ArgumentNullException: {0}", e->Message );
   }
   catch ( ArgumentException^ e ) 
   {
      Console::WriteLine( "ArgumentException: {0}", e->Message );
   }
   catch ( SecurityException^ e ) 
   {
      Console::WriteLine( "SecurityException: {0}", e->Message );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "Exception: {0}", e->Message );
   }
}
import System.*;
import System.Reflection.*;
import System.Security.*;

public class MyClass1
{
    public MyClass1(int i)
    {
    } //MyClass1

    public static void main(String[] args)
    {
        try {
            Type myType = MyClass1.class.ToType();
            Type types[] = new Type[1];
            types.set_Item(0, int.class.ToType());
            // Get the public instance constructor that takes an
            // integer parameter.
            ConstructorInfo constructorInfoObj =
                myType.GetConstructor(BindingFlags.Instance|BindingFlags.Public,
                null, CallingConventions.HasThis, types, null);
            if (constructorInfoObj != null) {
                Console.WriteLine("The constructor of MyClass1 that is a public "
                    + "instance method and takes an integer as a parameter is: ");
                Console.WriteLine(constructorInfoObj.ToString());
            }
            else {
                Console.WriteLine("The constructor of MyClass1 that is a "
                    + "public instance method and takes an integer "
                    + "as a parameter is not available.");
            }
        }
        catch (ArgumentNullException e) {
            Console.WriteLine("ArgumentNullException: " + e.get_Message());
        }
        catch (ArgumentException e) {
            Console.WriteLine("ArgumentException: " + e.get_Message());
        }
        catch (SecurityException e) {
            Console.WriteLine("SecurityException: " + e.get_Message());
        }
        catch (System.Exception e) {
            Console.WriteLine("Exception: " + e.get_Message());
        }
    } //main
} //MyClass1

继承层次结构

System.Object
   System.Reflection.MemberInfo
     System.Reflection.MethodBase
      System.Reflection.ConstructorInfo
         Microsoft.JScript.JSConstructor
         System.Reflection.Emit.ConstructorBuilder

线程安全

该类型对于多线程操作是安全的。

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

ConstructorInfo 成员
System.Reflection 命名空间