Type.GetProperties Metodo
Definizione
Overload
GetProperties() |
Restituisce tutte le proprietà pubbliche dell'oggetto Type corrente.Returns all the public properties of the current Type. |
GetProperties(BindingFlags) |
Quando ne viene eseguito l'override in una classe derivata, cerca le proprietà dell'oggetto Type corrente, usando i vincoli di binding specificati.When overridden in a derived class, searches for the properties of the current Type, using the specified binding constraints. |
GetProperties()
public:
cli::array <System::Reflection::PropertyInfo ^> ^ GetProperties();
public:
virtual cli::array <System::Reflection::PropertyInfo ^> ^ GetProperties();
public System.Reflection.PropertyInfo[] GetProperties ();
member this.GetProperties : unit -> System.Reflection.PropertyInfo[]
abstract member GetProperties : unit -> System.Reflection.PropertyInfo[]
override this.GetProperties : unit -> System.Reflection.PropertyInfo[]
Public Function GetProperties () As PropertyInfo()
Restituisce
Matrice di oggetti PropertyInfo che rappresentano tutte le proprietà pubbliche dell'oggetto Type corrente.An array of PropertyInfo objects representing all public properties of the current Type.
-oppure--or- Matrice vuota di tipo PropertyInfo, se l'oggetto Type corrente non possiede delle proprietà pubbliche.An empty array of type PropertyInfo, if the current Type does not have public properties.
Implementazioni
Esempio
Nell'esempio seguente viene illustrato l'uso del metodo GetProperties
.The following example demonstrates the use of the GetProperties
method.
array<PropertyInfo^>^myPropertyInfo;
// Get the properties of 'Type' class object.
myPropertyInfo = Type::GetType( "System.Type" )->GetProperties();
Console::WriteLine( "Properties of System.Type are:" );
for ( int i = 0; i < myPropertyInfo->Length; i++ )
{
Console::WriteLine( myPropertyInfo[ i ] );
}
PropertyInfo[] myPropertyInfo;
// Get the properties of 'Type' class object.
myPropertyInfo = Type.GetType("System.Type").GetProperties();
Console.WriteLine("Properties of System.Type are:");
for (int i = 0; i < myPropertyInfo.Length; i++)
{
Console.WriteLine(myPropertyInfo[i].ToString());
}
Dim myPropertyInfo() As PropertyInfo
' Get the properties of 'Type' class object.
myPropertyInfo = Type.GetType("System.Type").GetProperties()
Console.WriteLine("Properties of System.Type are:")
Dim i As Integer
For i = 0 To myPropertyInfo.Length - 1
Console.WriteLine(myPropertyInfo(i).ToString())
Next i
Commenti
La chiamata a questo overload equivale alla chiamata dell' GetProperties(BindingFlags) Overload con un bindingAttr
argomento uguale a BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public
in C# e BindingFlags.Instance Or BindingFlags.Static Or BindingFlags.Public
in Visual Basic.Calling this overload is equivalent to calling the GetProperties(BindingFlags) overload with a bindingAttr
argument equal to BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public
in C# and BindingFlags.Instance Or BindingFlags.Static Or BindingFlags.Public
in Visual Basic. Restituisce tutte le istanze pubbliche e le proprietà statiche, sia quelle definite dal tipo rappresentato dall'oggetto corrente sia Type quelle ereditate dai relativi tipi di base.It returns all public instance and static properties, both those defined by the type represented by the current Type object as well as those inherited from its base types.
Una proprietà viene considerata pubblica per la reflection se dispone di almeno una funzione di accesso pubblica.A property is considered public to reflection if it has at least one accessor that is public. In caso contrario, la proprietà viene considerata privata ed è necessario utilizzare BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static (in Visual Basic combinare i valori utilizzando Or
) per ottenerla.Otherwise the property is considered private, and you must use BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static (in Visual Basic, combine the values using Or
) to get it.
Il GetProperties metodo non restituisce le proprietà in un ordine particolare, ad esempio l'ordine alfabetico o di dichiarazione.The GetProperties method does not return properties in a particular order, such as alphabetical or declaration order. Il codice non deve dipendere dall'ordine in cui vengono restituite le proprietà, perché questo ordine varia.Your code must not depend on the order in which properties 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.
Se l'oggetto corrente Type rappresenta un tipo generico costruito, questo metodo restituisce gli PropertyInfo 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 PropertyInfo objects with the type parameters replaced by the appropriate type arguments.
Se l'oggetto corrente Type rappresenta un parametro di tipo nella definizione di un tipo o di un metodo generico, questo metodo esegue la ricerca delle proprietà del vincolo di classe.If the current Type represents a type parameter in the definition of a generic type or generic method, this method searches the properties of the class constraint.
Vedi anche
Si applica a
GetProperties(BindingFlags)
public:
abstract cli::array <System::Reflection::PropertyInfo ^> ^ GetProperties(System::Reflection::BindingFlags bindingAttr);
public abstract System.Reflection.PropertyInfo[] GetProperties (System.Reflection.BindingFlags bindingAttr);
abstract member GetProperties : System.Reflection.BindingFlags -> System.Reflection.PropertyInfo[]
Public MustOverride Function GetProperties (bindingAttr As BindingFlags) As PropertyInfo()
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 che rappresenta tutte le proprietà dell'oggetto Type corrente corrispondenti ai vincoli di associazione specificati.An array of objects representing all properties of the current Type that match the specified binding constraints.
-oppure--or- Matrice vuota di tipo PropertyInfo, se l'oggetto Type corrente non dispone di proprietà oppure se nessuna delle proprietà corrisponde ai vincoli di binding.An empty array of type PropertyInfo, if the current Type does not have properties, or if none of the properties match the binding constraints.
Implementazioni
Esempio
Nell'esempio seguente viene definita una classe denominata PropertyClass
che include sei proprietà: due sono public, una è privata, una è protetta, una è interna ( Friend
in Visual Basic) e una è protetta interna ( Protected Friend
in Visual Basic).The following example defines a class named PropertyClass
that includes six properties: two are public, one is private, one is protected, one is internal (Friend
in Visual Basic), and one is protected internal (Protected Friend
in Visual Basic). Vengono quindi visualizzate alcune informazioni di base sulle proprietà (il nome e il tipo della proprietà, se è di lettura/scrittura e la visibilità delle get
set
funzioni di accesso e) per le proprietà che corrispondono ai vincoli di binding specificati.It then displays some basic property information (the property name and type, whether it is read/write, and the visibility of its get
and set
accessors) for the properties that match the specified binding constraints.
using namespace System;
using namespace System::Reflection;
// Create a class having three properties.
public ref class PropertyClass
{
public:
property String^ Property1
{
String^ get()
{
return "hello";
}
}
property String^ Property2
{
String^ get()
{
return "hello";
}
}
protected:
property String^ Property3
{
String^ get()
{
return "hello";
}
}
private:
property int Property4
{
int get()
{
return 32;
}
}
internal:
property String^ Property5
{
String^ get()
{
return "value";
}
}
public protected:
property String^ Property6
{
String^ get()
{
return "value";
}
}
};
String^ GetVisibility(MethodInfo^ accessor)
{
if (accessor->IsPublic)
return "Public";
else if (accessor->IsPrivate)
return "Private";
else if (accessor->IsFamily)
return "Protected";
else if (accessor->IsAssembly)
return "Internal/Friend";
else
return "Protected Internal/Friend";
}
void DisplayPropertyInfo(array<PropertyInfo^>^ propInfos )
{
// Display information for all properties.
for each(PropertyInfo^ propInfo in propInfos) {
bool readable = propInfo->CanRead;
bool writable = propInfo->CanWrite;
Console::WriteLine(" Property name: {0}", propInfo->Name);
Console::WriteLine(" Property type: {0}", propInfo->PropertyType);
Console::WriteLine(" Read-Write: {0}", readable && writable);
if (readable) {
MethodInfo^ getAccessor = propInfo->GetMethod;
Console::WriteLine(" Visibility: {0}",
GetVisibility(getAccessor));
}
if (writable) {
MethodInfo^ setAccessor = propInfo->SetMethod;
Console::WriteLine(" Visibility: {0}",
GetVisibility(setAccessor));
}
Console::WriteLine();
}
}
void main()
{
Type^ myType = PropertyClass::typeid;
// Get the public properties.
array<PropertyInfo^>^propInfos = myType->GetProperties( static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Instance) );
Console::WriteLine("The number of public properties: {0}.\n",
propInfos->Length);
// Display the public properties.
DisplayPropertyInfo( propInfos );
// Get the non-public properties.
array<PropertyInfo^>^propInfos1 = myType->GetProperties( static_cast<BindingFlags>(BindingFlags::NonPublic | BindingFlags::Instance) );
Console::WriteLine("The number of non-public properties: {0}.\n",
propInfos1->Length);
// Display all the non-public properties.
DisplayPropertyInfo(propInfos1);
}
// The example displays the following output:
// The number of public properties: 2.
//
// Property name: Property2
// Property type: System.String
// Read-Write: False
// Visibility: Public
//
// Property name: Property1
// Property type: System.String
// Read-Write: False
// Visibility: Public
//
// The number of non-public properties: 4.
//
// Property name: Property6
// Property type: System.String
// Read-Write: False
// Visibility: Protected Internal/Friend
//
// Property name: Property5
// Property type: System.String
// Read-Write: False
// Visibility: Internal/Friend
//
// Property name: Property4
// Property type: System.Int32
// Read-Write: False
// Visibility: Private
//
// Property name: Property3
// Property type: System.String
// Read-Write: False
// Visibility: Protected
using System;
using System.Reflection;
// Create a class having six properties.
public class PropertyClass
{
public String Property1
{
get { return "hello"; }
}
public String Property2
{
get { return "hello"; }
}
protected String Property3
{
get { return "hello"; }
}
private Int32 Property4
{
get { return 32; }
}
internal String Property5
{
get { return "value"; }
}
protected internal String Property6
{
get { return "value"; }
}
}
public class Example
{
public static void Main()
{
Type t = typeof(PropertyClass);
// Get the public properties.
PropertyInfo[] propInfos = t.GetProperties(BindingFlags.Public|BindingFlags.Instance);
Console.WriteLine("The number of public properties: {0}.\n",
propInfos.Length);
// Display the public properties.
DisplayPropertyInfo(propInfos);
// Get the nonpublic properties.
PropertyInfo[] propInfos1 = t.GetProperties(BindingFlags.NonPublic|BindingFlags.Instance);
Console.WriteLine("The number of non-public properties: {0}.\n",
propInfos1.Length);
// Display all the nonpublic properties.
DisplayPropertyInfo(propInfos1);
}
public static void DisplayPropertyInfo(PropertyInfo[] propInfos)
{
// Display information for all properties.
foreach (var propInfo in propInfos) {
bool readable = propInfo.CanRead;
bool writable = propInfo.CanWrite;
Console.WriteLine(" Property name: {0}", propInfo.Name);
Console.WriteLine(" Property type: {0}", propInfo.PropertyType);
Console.WriteLine(" Read-Write: {0}", readable & writable);
if (readable) {
MethodInfo getAccessor = propInfo.GetMethod;
Console.WriteLine(" Visibility: {0}",
GetVisibility(getAccessor));
}
if (writable) {
MethodInfo setAccessor = propInfo.SetMethod;
Console.WriteLine(" Visibility: {0}",
GetVisibility(setAccessor));
}
Console.WriteLine();
}
}
public static String GetVisibility(MethodInfo accessor)
{
if (accessor.IsPublic)
return "Public";
else if (accessor.IsPrivate)
return "Private";
else if (accessor.IsFamily)
return "Protected";
else if (accessor.IsAssembly)
return "Internal/Friend";
else
return "Protected Internal/Friend";
}
}
// The example displays the following output:
// The number of public properties: 2.
//
// Property name: Property1
// Property type: System.String
// Read-Write: False
// Visibility: Public
//
// Property name: Property2
// Property type: System.String
// Read-Write: False
// Visibility: Public
//
// The number of non-public properties: 4.
//
// Property name: Property3
// Property type: System.String
// Read-Write: False
// Visibility: Protected
//
// Property name: Property4
// Property type: System.Int32
// Read-Write: False
// Visibility: Private
//
// Property name: Property5
// Property type: System.String
// Read-Write: False
// Visibility: Internal/Friend
//
// Property name: Property6
// Property type: System.String
// Read-Write: False
// Visibility: Protected Internal/Friend
Imports System.Reflection
' Create a class having six properties.
Public Class PropertyClass
Public ReadOnly Property Property1() As String
Get
Return "hello"
End Get
End Property
Public ReadOnly Property Property2() As String
Get
Return "hello"
End Get
End Property
Protected ReadOnly Property Property3() As String
Get
Return "hello"
End Get
End Property
Private ReadOnly Property Property4 As Integer
Get
Return 32
End Get
End Property
Friend ReadOnly Property Property5 As String
Get
Return "value"
End Get
End Property
Protected Friend ReadOnly Property Property6 As String
Get
Return "value"
End Get
End Property
End Class
Public Module Example
Public Sub Main()
Dim t As Type = GetType(PropertyClass)
' Get the public properties.
Dim propInfos As PropertyInfo() = t.GetProperties(BindingFlags.Public Or BindingFlags.Instance)
Console.WriteLine("The number of public properties: {0}",
propInfos.Length)
Console.WriteLine()
' Display the public properties.
DisplayPropertyInfo(propInfos)
' Get the non-public properties.
Dim propInfos1 As PropertyInfo() = t.GetProperties(BindingFlags.NonPublic Or BindingFlags.Instance)
Console.WriteLine("The number of non-public properties: {0}",
propInfos1.Length)
Console.WriteLine()
' Display all the non-public properties.
DisplayPropertyInfo(propInfos1)
End Sub
Public Sub DisplayPropertyInfo(ByVal propInfos() As PropertyInfo)
' Display information for all properties.
For Each propInfo In propInfos
Dim readable As Boolean = propInfo.CanRead
Dim writable As Boolean = propInfo.CanWrite
Console.WriteLine(" Property name: {0}", propInfo.Name)
Console.WriteLine(" Property type: {0}", propInfo.PropertyType)
Console.WriteLine(" Read-Write: {0}", readable And writable)
If readable Then
Dim getAccessor As MethodInfo = propInfo.GetMethod
Console.WriteLine(" Visibility: {0}",
GetVisibility(getAccessor))
End If
If writable Then
Dim setAccessor As MethodInfo = propInfo.SetMethod
Console.WriteLine(" Visibility: {0}",
GetVisibility(setAccessor))
End If
Console.WriteLine()
Next
End Sub
Public Function GetVisibility(accessor As MethodInfo) As String
If accessor.IsPublic Then
Return "Public"
ElseIf accessor.IsPrivate Then
Return "Private"
Else If accessor.IsFamily Then
Return "Protected"
Else If accessor.IsAssembly Then
Return "Internal/Friend"
Else
Return "Protected Internal/Friend"
End If
End Function
End Module
' The example displays the following output:
' The number of public properties: 2
'
' Property name: Property1
' Property type: System.String
' Read-Write: False
' Visibility: Public
'
' Property name: Property2
' Property type: System.String
' Read-Write: False
' Visibility: Public
'
' The number of non-public properties: 4
'
' Property name: Property3
' Property type: System.String
' Read-Write: False
' Visibility: Protected
'
' Property name: Property4
' Property type: System.Int32
' Read-Write: False
' Visibility: Private
'
' Property name: Property5
' Property type: System.String
' Read-Write: False
' Visibility: Internal/Friend
'
' Property name: Property6
' Property type: System.String
' Read-Write: False
' Visibility: Protected Internal/Friend
Commenti
Affinché l' GetProperties(BindingFlags)
Overload recuperi correttamente le informazioni sulle proprietà, l' bindingAttr
argomento deve includere almeno uno di BindingFlags.Instance e BindingFlags.Static , insieme ad almeno uno dei BindingFlags.NonPublic e BindingFlags.Public .For the GetProperties(BindingFlags)
overload to successfully retrieve property information, the bindingAttr
argument must include at least one of BindingFlags.Instance and BindingFlags.Static, along with at least one of BindingFlags.NonPublic and BindingFlags.Public.
BindingFlagsÈ possibile utilizzare i flag di filtro seguenti per definire le proprietà da includere nella ricerca:The following BindingFlags filter flags can be used to define which properties to include in the search:
Specificare
BindingFlags.Instance
per includere i metodi di istanza.SpecifyBindingFlags.Instance
to include instance methods.Specificare
BindingFlags.Static
per includere i metodi statici.SpecifyBindingFlags.Static
to include static methods.BindingFlags.Public
Consente di specificare di includere le proprietà pubbliche nella ricerca.SpecifyBindingFlags.Public
to include public properties in the search. Una proprietà viene considerata pubblica per la reflection se dispone di almeno una funzione di accesso pubblica.A property is considered public to reflection if it has at least one accessor that is public.Specificare
BindingFlags.NonPublic
per includere proprietà non pubbliche (proprietà private, interne e protette) nella ricerca.SpecifyBindingFlags.NonPublic
to include non-public properties (that is, private, internal, and protected properties) in the search. Vengono restituite solo le proprietà protette e interne delle classi di base. le proprietà private sulle classi base non vengono restituite.Only protected and internal properties on base classes are returned; private properties on base classes are not returned.BindingFlags.FlattenHierarchy
Consente di specificare di includerepublic
eprotected
membri statici nella gerarchiaprivate
. i membri statici nelle classi ereditate non sono inclusi.SpecifyBindingFlags.FlattenHierarchy
to includepublic
andprotected
static members up the hierarchy;private
static members in inherited classes are not included.Specificare
BindingFlags.Default
alone per restituire una PropertyInfo matrice vuota.SpecifyBindingFlags.Default
alone to return an empty PropertyInfo array.
I BindingFlags flag di modifica seguenti possono essere utilizzati per modificare il funzionamento della ricerca:The following BindingFlags modifier flags can be used to change how the search works:
BindingFlags.DeclaredOnly
per eseguire la ricerca solo delle proprietà dichiarate in Type , non delle proprietà ereditate semplicemente.BindingFlags.DeclaredOnly
to search only the properties declared on the Type, not properties that were simply inherited.
Per altre informazioni, vedere System.Reflection.BindingFlags.See System.Reflection.BindingFlags for more information.
Il GetProperties metodo non restituisce le proprietà in un ordine particolare, ad esempio l'ordine alfabetico o di dichiarazione.The GetProperties method does not return properties in a particular order, such as alphabetical or declaration order. Il codice non deve dipendere dall'ordine in cui vengono restituite le proprietà, perché questo ordine varia.Your code must not depend on the order in which properties are returned, because that order varies.
Se l'oggetto corrente Type rappresenta un tipo generico costruito, questo metodo restituisce gli PropertyInfo 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 PropertyInfo objects with the type parameters replaced by the appropriate type arguments.
Se l'oggetto corrente Type rappresenta un parametro di tipo nella definizione di un tipo o di un metodo generico, questo metodo esegue la ricerca delle proprietà del vincolo di classe.If the current Type represents a type parameter in the definition of a generic type or generic method, this method searches the properties of the class constraint.
Vedi anche
- PropertyInfo
- BindingFlags
- DefaultBinder
- GetProperty(String, BindingFlags, Binder, Type, Type[], ParameterModifier[])