PropertyInfo.GetAccessors Méthode
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Retourne un tableau des accesseurs et set des get accesseurs sur cette propriété.
Surcharges
| Nom | Description |
|---|---|
| GetAccessors() |
Retourne un tableau dont les éléments reflètent le public |
| GetAccessors(Boolean) |
Retourne un tableau dont les éléments reflètent le public et, s’ils sont spécifiés, non publics |
GetAccessors()
Retourne un tableau dont les éléments reflètent le public get et set les accesseurs de la propriété reflétées par l’instance actuelle.
public:
virtual cli::array <System::Reflection::MethodInfo ^> ^ GetAccessors();
public:
cli::array <System::Reflection::MethodInfo ^> ^ GetAccessors();
public System.Reflection.MethodInfo[] GetAccessors();
abstract member GetAccessors : unit -> System.Reflection.MethodInfo[]
override this.GetAccessors : unit -> System.Reflection.MethodInfo[]
member this.GetAccessors : unit -> System.Reflection.MethodInfo[]
Public Function GetAccessors () As MethodInfo()
Retours
Tableau d’objets MethodInfo qui reflètent le public get et set les accesseurs de la propriété reflétées par l’instance actuelle, s’ils sont trouvés ; sinon, cette méthode retourne un tableau avec zéro (0) éléments.
Implémente
Exemples
L’exemple suivant récupère les accesseurs publics de la ClassWithProperty.Caption propriété et affiche des informations à leur sujet. Il appelle également la Invoke méthode du setter pour définir la valeur de propriété et le getter pour récupérer la valeur de la propriété.
using System;
using System.Reflection;
// Define a property.
public class ClassWithProperty
{
private string _caption = "A Default caption";
public string Caption
{
get { return _caption; }
set { if(_caption != value) _caption = value; }
}
}
class Example
{
public static void Main()
{
ClassWithProperty test = new ClassWithProperty();
Console.WriteLine("The Caption property: {0}", test.Caption);
Console.WriteLine("----------");
// Get the type and PropertyInfo.
Type t = Type.GetType("ClassWithProperty");
PropertyInfo propInfo = t.GetProperty("Caption");
// Get the public GetAccessors method.
MethodInfo[] methInfos = propInfo.GetAccessors();
Console.WriteLine("There are {0} accessors.",
methInfos.Length);
for(int ctr = 0; ctr < methInfos.Length; ctr++) {
MethodInfo m = methInfos[ctr];
Console.WriteLine("Accessor #{0}:", ctr + 1);
Console.WriteLine(" Name: {0}", m.Name);
Console.WriteLine(" Visibility: {0}", GetVisibility(m));
Console.Write(" Property Type: ");
// Determine if this is the property getter or setter.
if (m.ReturnType == typeof(void)) {
Console.WriteLine("Setter");
Console.WriteLine(" Setting the property value.");
// Set the value of the property.
m.Invoke(test, new object[] { "The Modified Caption" } );
}
else {
Console.WriteLine("Getter");
// Get the value of the property.
Console.WriteLine(" Property Value: {0}",
m.Invoke(test, new object[] {} ));
}
}
Console.WriteLine("----------");
Console.WriteLine("The Caption property: {0}", test.Caption);
}
static string GetVisibility(MethodInfo m)
{
string visibility = "";
if (m.IsPublic)
return "Public";
else if (m.IsPrivate)
return "Private";
else
if (m.IsFamily)
visibility = "Protected ";
else if (m.IsAssembly)
visibility += "Assembly";
return visibility;
}
}
// The example displays the following output:
// The Caption property: A Default caption
// ----------
// There are 2 accessors.
// Accessor #1:
// Name: get_Caption
// Visibility: Public
// Property Type: Getter
// Property Value: A Default caption
// Accessor #2:
// Name: set_Caption
// Visibility: Public
// Property Type: Setter
// Setting the property value.
// ----------
// The Caption property: The Modified Caption
Imports System.Reflection
' Define a property.
Public Class ClassWithProperty
Private _caption As String = "A Default caption"
Public Property Caption As String
Get
Return _caption
End Get
Set
If _caption <> value Then _caption = value
End Set
End Property
End Class
Module Example
Public Sub Main()
Dim test As New ClassWithProperty()
Console.WriteLine("The Caption property: {0}", test.Caption)
Console.WriteLine("----------")
' Get the type and PropertyInfo.
Dim t As Type = Type.GetType("ClassWithProperty")
Dim propInfo As PropertyInfo = t.GetProperty("Caption")
' Get all the accessors.
Dim methInfos() As MethodInfo = propInfo.GetAccessors()
Console.WriteLine("There are {0} accessors.",
methInfos.Length)
For ctr As Integer = 0 To methInfos.Length - 1
Dim m As MethodInfo = methInfos(ctr)
Console.WriteLine("Accessor #{0}:", ctr + 1)
Console.WriteLine(" Name: {0}", m.Name)
Console.WriteLine(" Visibility: {0}", GetVisibility(m))
Console.Write(" Property Type: ")
' Determine if this is the property getter or setter.
'' If (m.ReturnType == typeof(void))
If m.ReturnType Is GetType(Void) Then
Console.WriteLine("Setter")
Console.WriteLine(" Setting the property value.")
' Set the value of the property.
m.Invoke(test, { "The Modified Caption" } )
Else
Console.WriteLine("Getter")
' Get the value of the property.
Console.WriteLine(" Property Value: {0}",
m.Invoke(test, {} ))
End If
Next
Console.WriteLine("----------")
Console.WriteLine("The Caption property: {0}", test.Caption)
End Sub
Private Function GetVisibility(m As MethodInfo) As String
Dim visibility As String = ""
If m.IsPublic Then
Return "Public"
ElseIf m.IsPrivate Then
Return "Private"
Else
If m.IsFamily Then
visibility = "Protected "
ElseIf m.IsAssembly Then
visibility += "Assembly"
End If
End If
Return visibility
End Function
End Module
' The example displays the following output:
' The Caption property: A Default caption
' ----------
' There are 2 accessors.
' Accessor #1:
' Name: get_Caption
' Visibility: Public
' Property Type: Getter
' Property Value: A Default caption
' Accessor #2:
' Name: set_Caption
' Visibility: Public
' Property Type: Setter
' Setting the property value.
' ----------
' The Caption property: The Modified Caption
Remarques
Pour appeler la GetAccessors méthode :
1.Obtenir un Type objet qui représente la classe. 2.À partir de l’objet Type , obtenez l’objet PropertyInfo . 3.À partir de l’objet PropertyInfo , appelez la GetAccessors méthode.
S’applique à
GetAccessors(Boolean)
Retourne un tableau dont les éléments reflètent le public et, s’ils sont spécifiés, non publics get et set accesseurs de la propriété reflétées par l’instance actuelle.
public:
abstract cli::array <System::Reflection::MethodInfo ^> ^ GetAccessors(bool nonPublic);
public abstract System.Reflection.MethodInfo[] GetAccessors(bool nonPublic);
abstract member GetAccessors : bool -> System.Reflection.MethodInfo[]
Public MustOverride Function GetAccessors (nonPublic As Boolean) As MethodInfo()
Paramètres
- nonPublic
- Boolean
Indique si les méthodes non publiques doivent être retournées dans le tableau retourné.
true si des méthodes non publiques doivent être incluses ; sinon, false.
Retours
Tableau dont les éléments reflètent les accesseurs et set les get accesseurs de la propriété reflétées par l’instance actuelle. Si nonPublic c’est truele cas, ce tableau contient des accesseurs et des accesseurs publics et non publics getset . Si nonPublic c’est falsele cas, ce tableau contient uniquement des accesseurs publics et set publicsget. Si aucun accesseur avec la visibilité spécifiée est trouvé, cette méthode retourne un tableau avec zéro (0) éléments.
Implémente
Exemples
L’exemple suivant récupère les accesseurs de la ClassWithProperty.Caption propriété et affiche des informations à leur sujet. Il appelle également la Invoke méthode du setter pour définir la valeur de propriété et le getter pour récupérer la valeur de la propriété.
using System;
using System.Reflection;
// Define a property.
public class ClassWithProperty
{
private string _caption = "A Default caption";
public string Caption
{
get { return _caption; }
set { if(_caption != value) _caption = value; }
}
}
class Example
{
public static void Main()
{
ClassWithProperty test = new ClassWithProperty();
Console.WriteLine("The Caption property: {0}", test.Caption);
Console.WriteLine("----------");
// Get the type and PropertyInfo.
Type t = Type.GetType("ClassWithProperty");
PropertyInfo propInfo = t.GetProperty("Caption");
// Get the public GetAccessors method.
MethodInfo[] methInfos = propInfo.GetAccessors(true);
Console.WriteLine("There are {0} accessors.",
methInfos.Length);
for(int ctr = 0; ctr < methInfos.Length; ctr++) {
MethodInfo m = methInfos[ctr];
Console.WriteLine("Accessor #{0}:", ctr + 1);
Console.WriteLine(" Name: {0}", m.Name);
Console.WriteLine(" Visibility: {0}", GetVisibility(m));
Console.Write(" Property Type: ");
// Determine if this is the property getter or setter.
if (m.ReturnType == typeof(void)) {
Console.WriteLine("Setter");
Console.WriteLine(" Setting the property value.");
// Set the value of the property.
m.Invoke(test, new object[] { "The Modified Caption" } );
}
else {
Console.WriteLine("Getter");
// Get the value of the property.
Console.WriteLine(" Property Value: {0}",
m.Invoke(test, new object[] {} ));
}
}
Console.WriteLine("----------");
Console.WriteLine("The Caption property: {0}", test.Caption);
}
static string GetVisibility(MethodInfo m)
{
string visibility = "";
if (m.IsPublic)
return "Public";
else if (m.IsPrivate)
return "Private";
else
if (m.IsFamily)
visibility = "Protected ";
else if (m.IsAssembly)
visibility += "Assembly";
return visibility;
}
}
// The example displays the following output:
// The Caption property: A Default caption
// ----------
// There are 2 accessors.
// Accessor #1:
// Name: get_Caption
// Visibility: Public
// Property Type: Getter
// Property Value: A Default caption
// Accessor #2:
// Name: set_Caption
// Visibility: Public
// Property Type: Setter
// Setting the property value.
// ----------
// The Caption property: The Modified Caption
Imports System.Reflection
' Define a property.
Public Class ClassWithProperty
Private _caption As String = "A Default caption"
Public Property Caption As String
Get
Return _caption
End Get
Set
If _caption <> value Then _caption = value
End Set
End Property
End Class
Module Example
Public Sub Main()
Dim test As New ClassWithProperty()
Console.WriteLine("The Caption property: {0}", test.Caption)
Console.WriteLine("----------")
' Get the type and PropertyInfo.
Dim t As Type = Type.GetType("ClassWithProperty")
Dim propInfo As PropertyInfo = t.GetProperty("Caption")
' Get all the accessors.
Dim methInfos() As MethodInfo = propInfo.GetAccessors(True)
Console.WriteLine("There are {0} accessors.",
methInfos.Length)
For ctr As Integer = 0 To methInfos.Length - 1
Dim m As MethodInfo = methInfos(ctr)
Console.WriteLine("Accessor #{0}:", ctr + 1)
Console.WriteLine(" Name: {0}", m.Name)
Console.WriteLine(" Visibility: {0}", GetVisibility(m))
Console.Write(" Property Type: ")
' Determine if this is the property getter or setter.
'' If (m.ReturnType == typeof(void))
If m.ReturnType Is GetType(Void) Then
Console.WriteLine("Setter")
Console.WriteLine(" Setting the property value.")
' Set the value of the property.
m.Invoke(test, { "The Modified Caption" } )
Else
Console.WriteLine("Getter")
' Get the value of the property.
Console.WriteLine(" Property Value: {0}",
m.Invoke(test, {} ))
End If
Next
Console.WriteLine("----------")
Console.WriteLine("The Caption property: {0}", test.Caption)
End Sub
Private Function GetVisibility(m As MethodInfo) As String
Dim visibility As String = ""
If m.IsPublic Then
Return "Public"
ElseIf m.IsPrivate Then
Return "Private"
Else
If m.IsFamily Then
visibility = "Protected "
ElseIf m.IsAssembly Then
visibility += "Assembly"
End If
End If
Return visibility
End Function
End Module
' The example displays the following output:
' The Caption property: A Default caption
' ----------
' There are 2 accessors.
' Accessor #1:
' Name: get_Caption
' Visibility: Public
' Property Type: Getter
' Property Value: A Default caption
' Accessor #2:
' Name: set_Caption
' Visibility: Public
' Property Type: Setter
' Setting the property value.
' ----------
' The Caption property: The Modified Caption
Remarques
Pour appeler la GetAccessors méthode :
- Obtient un Type objet qui représente la classe.
- À partir de l’objet Type , obtenez l’objet PropertyInfo .
- À partir de l’objet PropertyInfo, appelez la méthode GetAccessors.