PropertyInfo.GetAccessors Methode
Definition
Wichtig
Einige Informationen beziehen sich auf Vorabversionen, die vor dem Release ggf. grundlegend überarbeitet werden. Microsoft übernimmt hinsichtlich der hier bereitgestellten Informationen keine Gewährleistungen, seien sie ausdrücklich oder konkludent.
Gibt ein Array der get Accessoren für set diese Eigenschaft zurück.
Überlädt
| Name | Beschreibung |
|---|---|
| GetAccessors() |
Gibt ein Array zurück, dessen Elemente die öffentlichen |
| GetAccessors(Boolean) |
Gibt ein Array zurück, dessen Elemente die öffentliche und, falls angegeben, nicht öffentliche |
GetAccessors()
Gibt ein Array zurück, dessen Elemente die öffentlichen get und set Accessoren der Eigenschaft widerspiegeln, die von der aktuellen Instanz wiedergegeben werden.
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()
Gibt zurück
Ein Array von MethodInfo Objekten, die die öffentlichen get und set Accessoren der Eigenschaft widerspiegeln, die von der aktuellen Instanz widerspiegelt werden, falls gefunden. Andernfalls gibt diese Methode ein Array mit Nullelementen (0) zurück.
Implementiert
Beispiele
Im folgenden Beispiel werden die öffentlichen Accessoren der ClassWithProperty.Caption Eigenschaft abgerufen und Informationen zu dieser Eigenschaft angezeigt. Außerdem wird die Invoke Methode des Setters aufgerufen, um den Eigenschaftswert und den Getter festzulegen, um den Eigenschaftswert abzurufen.
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
Hinweise
So rufen Sie die GetAccessors Methode auf:
1.Abrufen eines Type Objekts, das die Klasse darstellt. 2.Rufen Sie aus dem Type Objekt das PropertyInfo Objekt ab. 3.Rufen Sie die GetAccessors Methode vom PropertyInfo Objekt auf.
Gilt für:
GetAccessors(Boolean)
Gibt ein Array zurück, dessen Elemente die öffentliche und, falls angegeben, nicht öffentliche get und set Accessoren der Eigenschaft widerspiegeln, die von der aktuellen Instanz widergespiegelt werden.
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()
Parameter
- nonPublic
- Boolean
Gibt an, ob nicht öffentliche Methoden im zurückgegebenen Array zurückgegeben werden sollen.
truewenn nicht öffentliche Methoden einbezogen werden sollen; andernfalls . false
Gibt zurück
Ein Array, dessen Elemente die get und set Accessoren der Eigenschaft widerspiegeln, die von der aktuellen Instanz widerzuspiegeln sind. Ist nonPublic dies der trueFall, enthält dieses Array öffentliche und nicht öffentliche get und set Accessoren. Ist nonPublic dies der Fall false, enthält dieses Array nur öffentliche get und set Accessoren. Wenn keine Accessoren mit der angegebenen Sichtbarkeit gefunden werden, gibt diese Methode ein Array mit Nullelementen (0) zurück.
Implementiert
Beispiele
Im folgenden Beispiel werden die Accessoren der ClassWithProperty.Caption Eigenschaft abgerufen und Informationen darüber angezeigt. Außerdem wird die Invoke Methode des Setters aufgerufen, um den Eigenschaftswert und den Getter festzulegen, um den Eigenschaftswert abzurufen.
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
Hinweise
So rufen Sie die GetAccessors Methode auf:
- Ruft ein Type Objekt ab, das die Klasse darstellt.
- Rufen Sie das Objekt aus dem TypePropertyInfo Objekt ab.
- Rufen Sie die Methode aus dem PropertyInfoGetAccessors Objekt auf.