Type.GetConstructors Metodo
Definizione
Overload
GetConstructors() |
Restituisce tutti i costruttori di tipo pubblico definiti per l'oggetto Type corrente.Returns all the public constructors defined for the current Type. |
GetConstructors(BindingFlags) |
Quando ne viene eseguito l'override in una classe derivata, cerca i costruttori definiti per l'oggetto Type corrente, usando i |
Esempio
Questo esempio mostra l'output dell' GetConstructors() Overload da una classe che dispone di due costruttori di istanza e un costruttore statico.This example shows the output of the GetConstructors() overload from a class that has two instance constructors and one static constructor.
using namespace System;
using namespace System::Reflection;
public ref class t
{
public:
t(){}
static t(){}
t( int /*i*/ ){}
};
int main()
{
array<ConstructorInfo^>^p = t::typeid->GetConstructors();
Console::WriteLine( p->Length );
for ( int i = 0; i < p->Length; i++ )
{
Console::WriteLine( p[ i ]->IsStatic );
}
}
using System;
using System.Reflection;
public class t {
public t() {}
static t() {}
public t(int i) {}
public static void Main() {
ConstructorInfo[] p = typeof(t).GetConstructors();
Console.WriteLine(p.Length);
for (int i=0;i<p.Length;i++) {
Console.WriteLine(p[i].IsStatic);
}
}
}
Imports System.Reflection
Public Class t
Public Sub New()
End Sub
Shared Sub New()
End Sub
Public Sub New(i As Integer)
End Sub
Public Shared Sub Main()
Dim p As ConstructorInfo() = GetType(t).GetConstructors()
Console.WriteLine(p.Length)
Dim i As Integer
For i = 0 To p.Length - 1
Console.WriteLine(p(i).IsStatic)
Next i
End Sub
End Class
L'output di questo codice è:The output of this code is:
2
False
False
Poiché l' GetConstructors Overload utilizza solo Public e Instance , il costruttore statico non viene conteggiato dall' for
espressione né valutato da IsStatic
.Because the GetConstructors overload uses only Public and Instance, the static constructor is neither counted by the for
expression nor evaluated by IsStatic
.
Per trovare i costruttori statici, usare l' GetConstructors Overload e passargli la combinazione (OR logico) di BindingFlags.Public , BindingFlags.Static , BindingFlags.NonPublic , BindingFlags.Instance , come illustrato nell'esempio di codice seguente:To find static constructors, use the GetConstructors overload, and pass it the combination (logical OR) of BindingFlags.Public, BindingFlags.Static, BindingFlags.NonPublic, BindingFlags.Instance, as shown in the following code example:
using namespace System;
using namespace System::Reflection;
public ref class t
{
public:
t(){}
t( int /*i*/ ){}
static t(){}
};
int main()
{
array<ConstructorInfo^>^p = t::typeid->GetConstructors( static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Static | BindingFlags::NonPublic | BindingFlags::Instance) );
Console::WriteLine( p->Length );
for ( int i = 0; i < p->Length; i++ )
{
Console::WriteLine( p[ i ]->IsStatic );
}
}
using System;
using System.Reflection;
public class t {
public t() {}
static t() {}
public t(int i) {}
public static void Main() {
ConstructorInfo[] p = typeof(t).GetConstructors(
BindingFlags.Public | BindingFlags.Static |
BindingFlags.NonPublic | BindingFlags.Instance);
Console.WriteLine(p.Length);
for (int i=0;i<p.Length;i++) {
Console.WriteLine(p[i].IsStatic);
}
}
}
Imports System.Reflection
Public Class t
Public Sub New()
End Sub
Shared Sub New()
End Sub
Public Sub New(i As Integer)
End Sub
Public Shared Sub Main()
Dim p As ConstructorInfo() = GetType(T).GetConstructors( _
BindingFlags.Public Or _
BindingFlags.Static Or _
BindingFlags.NonPublic Or _
BindingFlags.Instance)
Console.WriteLine(p.Length)
Dim i As Integer
For i = 0 To p.Length - 1
Console.WriteLine(p(i).IsStatic)
Next i
End Sub
End Class
A questo punto l'output è:Now the output is:
3
False
True
False
GetConstructors()
public:
cli::array <System::Reflection::ConstructorInfo ^> ^ GetConstructors();
public:
virtual cli::array <System::Reflection::ConstructorInfo ^> ^ GetConstructors();
public System.Reflection.ConstructorInfo[] GetConstructors ();
[System.Runtime.InteropServices.ComVisible(true)]
public System.Reflection.ConstructorInfo[] GetConstructors ();
member this.GetConstructors : unit -> System.Reflection.ConstructorInfo[]
abstract member GetConstructors : unit -> System.Reflection.ConstructorInfo[]
override this.GetConstructors : unit -> System.Reflection.ConstructorInfo[]
[<System.Runtime.InteropServices.ComVisible(true)>]
abstract member GetConstructors : unit -> System.Reflection.ConstructorInfo[]
override this.GetConstructors : unit -> System.Reflection.ConstructorInfo[]
Public Function GetConstructors () As ConstructorInfo()
Restituisce
Matrice di oggetti ConstructorInfo che rappresentano tutti i costruttori di istanza pubblici definiti per l'oggetto Type corrente, senza l'inizializzatore di tipo (costruttore statico).An array of ConstructorInfo objects representing all the public instance constructors defined for the current Type, but not including the type initializer (static constructor). Se per l'oggetto Type corrente non sono definiti costruttori di istanza pubblici o se l'oggetto Type corrente rappresenta un parametro di tipo nella definizione di un tipo o di un metodo generico, viene restituita una matrice vuota di tipo ConstructorInfo.If no public instance constructors are defined for the current Type, or if the current Type represents a type parameter in the definition of a generic type or generic method, an empty array of type ConstructorInfo is returned.
Implementazioni
- Attributi
Commenti
Il GetConstructors metodo non restituisce costruttori in un ordine particolare, ad esempio l'ordine di dichiarazione.The GetConstructors method does not return constructors in a particular order, such as declaration order. Il codice non deve dipendere dall'ordine in cui vengono restituiti i costruttori, perché questo ordine varia.Your code must not depend on the order in which constructors are returned, because that order varies.
La tabella seguente mostra quali membri di una classe di base vengono restituiti dai Get
metodi durante la reflection su un tipo.The following table shows what members of a base class are returned by the Get
methods when reflecting on a type.
Tipo di membroMember Type | StaticStatic | Non staticoNon-Static |
---|---|---|
CostruttoreConstructor | NoNo | NoNo |
CampoField | NoNo | Sì.Yes. Un campo è sempre nascosto per nome e firma.A field is always hide-by-name-and-signature. |
EventoEvent | Non applicabileNot applicable | La regola Common Type System è che l'ereditarietà è identica a quella dei metodi che implementano la proprietà.The common type system rule is that the inheritance is the same as that of the methods that implement the property. La reflection considera le proprietà come nascoste per nome e firma.Reflection treats properties as hide-by-name-and-signature. Vedere la nota 2 di seguito.See note 2 below. |
MetodoMethod | NoNo | Sì.Yes. Un metodo (sia virtuale che non virtuale) può essere nascosto in base al nome o nascosto per nome e firma.A method (both virtual and non-virtual) can be hide-by-name or hide-by-name-and-signature. |
Tipo annidatoNested Type | NoNo | NoNo |
ProprietàProperty | Non applicabileNot applicable | La regola Common Type System è che l'ereditarietà è identica a quella dei metodi che implementano la proprietà.The common type system rule is that the inheritance is the same as that of the methods that implement the property. La reflection considera le proprietà come nascoste per nome e firma.Reflection treats properties as hide-by-name-and-signature. Vedere la nota 2 di seguito.See note 2 below. |
Hide-by-Name-and-signature prende in considerazione tutte le parti della firma, inclusi i modificatori personalizzati, i tipi restituiti, i tipi di parametro, le sentinelle e le convenzioni di chiamata non gestite.Hide-by-name-and-signature considers all of the parts of the signature, including custom modifiers, return types, parameter types, sentinels, and unmanaged calling conventions. Si tratta di un confronto binario.This is a binary comparison.
Per la reflection, le proprietà e gli eventi sono nascosti per nome e firma.For reflection, properties and events are hide-by-name-and-signature. Se si dispone di una proprietà con una funzione di accesso get e set nella classe di base, ma la classe derivata dispone solo di una funzione di accesso get, la proprietà della classe derivata nasconde la proprietà della classe base e non sarà possibile accedere al setter sulla classe di base.If you have a property with both a get and a set accessor in the base class, but the derived class has only a get accessor, the derived class property hides the base class property, and you will not be able to access the setter on the base class.
Gli attributi personalizzati non fanno parte del Common Type System.Custom attributes are not part of the common type system.
Questo overload del metodo chiama l' GetConstructors(BindingFlags) Overload del metodo con BindingFlags.Public | BindingFlags.Instance ( BindingFlags.Public Or
BindingFlags.Instance in Visual Basic).This method overload calls the GetConstructors(BindingFlags) method overload, with BindingFlags.Public | BindingFlags.Instance (BindingFlags.PublicOr
BindingFlags.Instance in Visual Basic). Non troverà inizializzatori di classe (costruttore statico).It will not find class initializers (static constructor). Per trovare gli inizializzatori di classe, usare un overload che accetta BindingFlags e specificare BindingFlags.Static | BindingFlags.NonPublic ( BindingFlags.Static Or
BindingFlags.NonPublic in Visual Basic).To find class initializers, use an overload that takes BindingFlags, and specify BindingFlags.Static | BindingFlags.NonPublic (BindingFlags.StaticOr
BindingFlags.NonPublic in Visual Basic). È anche possibile ottenere l'inizializzatore di classe usando la TypeInitializer Proprietà.You can also get the class initializer using the TypeInitializer property.
Se l'oggetto corrente Type rappresenta un tipo generico costruito, questo metodo restituisce gli ConstructorInfo oggetti con i parametri di tipo sostituiti dagli argomenti di tipo appropriati.If the current Type represents a constructed generic type, this method returns the ConstructorInfo objects with the type parameters replaced by the appropriate type arguments. Se, ad esempio, C<T>
la classe dispone di un costruttore C(T t1)
( Sub New(ByVal t1 As T)
in Visual Basic), GetConstructors la chiamata di su C<int>
restituisce un oggetto ConstructorInfo che rappresenta C(int t1)
in C# ( Sub New(ByVal t1 As Integer)
in Visual Basic).For example, if class C<T>
has a constructor C(T t1)
(Sub New(ByVal t1 As T)
in Visual Basic), calling GetConstructors on C<int>
returns a ConstructorInfo that represents C(int t1)
in C# (Sub New(ByVal t1 As Integer)
in Visual Basic).
Se l'oggetto corrente Type rappresenta un parametro di tipo generico, il GetConstructors metodo restituisce una matrice vuota.If the current Type represents a generic type parameter, the GetConstructors method returns an empty array.
Vedi anche
- ConstructorInfo
- GetConstructor(BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])
Si applica a
GetConstructors(BindingFlags)
public:
abstract cli::array <System::Reflection::ConstructorInfo ^> ^ GetConstructors(System::Reflection::BindingFlags bindingAttr);
public abstract System.Reflection.ConstructorInfo[] GetConstructors (System.Reflection.BindingFlags bindingAttr);
[System.Runtime.InteropServices.ComVisible(true)]
public abstract System.Reflection.ConstructorInfo[] GetConstructors (System.Reflection.BindingFlags bindingAttr);
abstract member GetConstructors : System.Reflection.BindingFlags -> System.Reflection.ConstructorInfo[]
[<System.Runtime.InteropServices.ComVisible(true)>]
abstract member GetConstructors : System.Reflection.BindingFlags -> System.Reflection.ConstructorInfo[]
Public MustOverride Function GetConstructors (bindingAttr As BindingFlags) As ConstructorInfo()
Parametri
- bindingAttr
- BindingFlags
Combinazione bit per bit di valori di enumerazione che specifica il modo in cui viene eseguita la ricerca.A bitwise combination of the enumeration values that specify how the search is conducted.
-oppure--or- Default per restituire una matrice vuota.Default to return an empty array.
Restituisce
Matrice di oggetti ConstructorInfo che rappresentano tutti i costruttori definiti per l'oggetto Type corrente corrispondente ai vincoli di associazione specificati, compreso l'inizializzatore di tipo, se definito.An array of ConstructorInfo objects representing all constructors defined for the current Type that match the specified binding constraints, including the type initializer if it is defined. Restituisce una matrice vuota di tipo ConstructorInfo se per l'oggetto Type corrente non sono definiti costruttori, se nessuno dei costruttori definiti corrisponde ai vincoli di associazione o se l'oggetto Type corrente rappresenta un parametro di tipo nella definizione di un tipo o di un metodo generico.Returns an empty array of type ConstructorInfo if no constructors are defined for the current Type, if none of the defined constructors match the binding constraints, or if the current Type represents a type parameter in the definition of a generic type or generic method.
Implementazioni
- Attributi
Commenti
bindingAttr
può essere usato per specificare se restituire solo i costruttori pubblici o i costruttori pubblici e non pubblici.bindingAttr
can be used to specify whether to return only public constructors or both public and non-public constructors.
I BindingFlags flag di filtro seguenti possono essere usati per definire i costruttori da includere nella ricerca:The following BindingFlags filter flags can be used to define which constructors to include in the search:
Specificare
BindingFlags.Static
insiemeBindingFlags.NonPublic
a per recuperare l'inizializzatore di classe (costruttore statico).SpecifyBindingFlags.Static
along withBindingFlags.NonPublic
to retrieve the class initializer (static constructor). È anche possibile ottenere l'inizializzatore di classe usando la TypeInitializer Proprietà.You can also get the class initializer using the TypeInitializer property.Specificare
BindingFlags.Instance
insieme a una o entrambe leBindingFlags.Public
istanze di eBindingFlags.NonPublic
per recuperare i costruttori di istanze.SpecifyBindingFlags.Instance
along with one or both ofBindingFlags.Public
andBindingFlags.NonPublic
to retrieve instance constructors.
Per altre informazioni, vedere System.Reflection.BindingFlags.See System.Reflection.BindingFlags for more information.
Il GetConstructors metodo non restituisce costruttori in un ordine particolare, ad esempio l'ordine di dichiarazione.The GetConstructors method does not return constructors in a particular order, such as declaration order. Il codice non deve dipendere dall'ordine in cui vengono restituiti i costruttori, perché questo ordine varia.Your code must not depend on the order in which constructors are returned, because that order varies.
Se l'oggetto corrente Type rappresenta un tipo generico costruito, questo metodo restituisce gli ConstructorInfo oggetti con i parametri di tipo sostituiti dagli argomenti di tipo appropriati.If the current Type represents a constructed generic type, this method returns the ConstructorInfo objects with the type parameters replaced by the appropriate type arguments. Se, ad esempio, C<T>
la classe dispone di un costruttore C(T t1)
( Sub New(ByVal t1 As T)
in Visual Basic), GetConstructors la chiamata di su C<int>
restituisce un oggetto ConstructorInfo che rappresenta C(int t1)
in C# ( Sub New(ByVal t1 As Integer)
in Visual Basic).For example, if class C<T>
has a constructor C(T t1)
(Sub New(ByVal t1 As T)
in Visual Basic), calling GetConstructors on C<int>
returns a ConstructorInfo that represents C(int t1)
in C# (Sub New(ByVal t1 As Integer)
in Visual Basic).
Se l'oggetto corrente Type rappresenta un parametro di tipo generico, il GetConstructors metodo restituisce una matrice vuota.If the current Type represents a generic type parameter, the GetConstructors method returns an empty array.
Vedi anche
- ConstructorInfo
- BindingFlags
- DefaultBinder
- GetConstructor(BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])