Dieser Artikel wurde noch nicht bewertet - Dieses Thema bewerten.

ConstructorInfo-Klasse

Ermittelt die Attribute eines Klassenkonstruktors und bietet Zugriff auf Metadaten des Konstruktors.

Namespace: System.Reflection
Assembly: mscorlib (in mscorlib.dll)

[SerializableAttribute] 
[ComVisibleAttribute(true)] 
[ClassInterfaceAttribute(ClassInterfaceType.None)] 
public abstract class ConstructorInfo : 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 wird zum Ermitteln der Attribute eines Konstruktors sowie zum Aufrufen eines Konstruktors verwendet. Objekte werden durch einen Aufruf von Invoke für eine ConstructorInfo erstellt, die entweder von der GetConstructors-Methode oder der GetConstructor-Methode eines Type-Objekts zurückgegeben wird.

HinweisHinweis

ConstructorInfo erbt mehrere Member von MethodBase, z. B. IsGenericMethod, mit dem generische Methoden untersucht werden können. In .NET Framework, Version 2.0, können Konstruktoren nicht generisch sein, deshalb geben diese Member false oder NULL (Nothing in Visual Basic) zurück.

Hinweise für Erben Wenn Sie ConstructorInfo vererben, müssen Sie die folgende Memberüberladung überschreiben: Invoke(BindingFlags,Binder,Object[],CultureInfo).

Im folgenden Beispiel wird ConstructorInfo mit GetConstructor und BindingFlags für die Suche nach Konstruktoren verwendet, die mit den angegebenen Suchkriterien übereinstimmen.

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);
        }
    }
}

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

Dieser Typ ist sicher bei Multithreadoperationen.

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0
Fanden Sie dies hilfreich?
(1500 verbleibende Zeichen)
© 2013 Microsoft. Alle Rechte vorbehalten.