PropertyInfo.GetSetMethod 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 MethodInfo représentant l’accesseur set pour cette propriété.
Surcharges
| Nom | Description |
|---|---|
| GetSetMethod(Boolean) |
En cas de substitution dans une classe dérivée, retourne l’accesseur |
| GetSetMethod() |
Retourne l’accesseur public |
GetSetMethod(Boolean)
En cas de substitution dans une classe dérivée, retourne l’accesseur set pour cette propriété.
public:
abstract System::Reflection::MethodInfo ^ GetSetMethod(bool nonPublic);
public abstract System.Reflection.MethodInfo GetSetMethod(bool nonPublic);
abstract member GetSetMethod : bool -> System.Reflection.MethodInfo
Public MustOverride Function GetSetMethod (nonPublic As Boolean) As MethodInfo
Paramètres
- nonPublic
- Boolean
Indique si l’accesseur doit être retourné s’il n’est pas public.
true si un accesseur non public doit être retourné ; sinon, false.
Retours
Méthode de Set cette propriété, ou null, comme indiqué dans le tableau suivant.
| Valeur | Condition |
|---|---|
Méthode Set de cette propriété.
| L’accesseur set est public, OR nonPublic est true et l’accesseur set n’est pas public.
|
null |
nonPublic est true, mais la propriété est en lecture seule, OR nonPublic est false et l’accesseur set n’est pas public, OR il n’y a pas d’accesseur set .
|
Implémente
Exceptions
La méthode demandée n’est pas publique et l’appelant n’a ReflectionPermission pas à réfléchir à cette méthode non publique.
Exemples
L’exemple suivant affiche l’accesseur set pour la propriété spécifiée.
using System;
using System.Reflection;
// Define a property.
public class Myproperty
{
private string caption = "A Default caption";
public string Caption
{
get{return caption;}
set {if(caption!=value) {caption = value;}
}
}
}
class Mypropertyinfo
{
public static int Main()
{
Console.WriteLine ("\nReflection.PropertyInfo");
// Get the type and PropertyInfo for two separate properties.
Type MyTypea = Type.GetType("Myproperty");
PropertyInfo Mypropertyinfoa = MyTypea.GetProperty("Caption");
Type MyTypeb = Type.GetType("System.Text.StringBuilder");
PropertyInfo Mypropertyinfob = MyTypeb.GetProperty("Length");
// Get and display the GetSetMethod method for each property.
MethodInfo Mygetmethodinfoa = Mypropertyinfoa.GetSetMethod();
Console.Write ("\nSetAccessor for " + Mypropertyinfoa.Name
+ " returns a " + Mygetmethodinfoa.ReturnType);
MethodInfo Mygetmethodinfob = Mypropertyinfob.GetSetMethod();
Console.Write ("\nSetAccessor for " + Mypropertyinfob.Name
+ " returns a " + Mygetmethodinfob.ReturnType);
// Display the GetSetMethod without using the MethodInfo.
Console.Write ("\n\n" + MyTypea.FullName + "."
+ Mypropertyinfoa.Name + " GetSetMethod - "
+ Mypropertyinfoa.GetSetMethod());
Console.Write ("\n" + MyTypeb.FullName + "."
+ Mypropertyinfob.Name + " GetSetMethod - "
+ Mypropertyinfob.GetSetMethod());
return 0;
}
}
Imports System.Reflection
' Define a property.
Public Class Myproperty
Private myCaption As String = "A Default caption"
Public Property Caption() As String
Get
Return myCaption
End Get
Set(ByVal Value As String)
If myCaption <> value Then
myCaption = value
End If
End Set
End Property
End Class
Class Mypropertyinfo
Public Shared Function Main() As Integer
Console.WriteLine(ControlChars.CrLf & "Reflection.PropertyInfo")
' Get the type and PropertyInfo for two separate properties.
Dim MyTypea As Type = Type.GetType("Myproperty")
Dim Mypropertyinfoa As PropertyInfo = MyTypea.GetProperty("Caption")
Dim MyTypeb As Type = Type.GetType("System.Text.StringBuilder")
Dim Mypropertyinfob As PropertyInfo = MyTypeb.GetProperty("Length")
' Get and display the GetSetMethod method for each property.
Dim Mygetmethodinfoa As MethodInfo = Mypropertyinfoa.GetSetMethod()
Console.WriteLine("SetAccessor for " & Mypropertyinfoa.Name & _
" returns a " & Mygetmethodinfoa.ReturnType.ToString())
Dim Mygetmethodinfob As MethodInfo = Mypropertyinfob.GetSetMethod()
Console.WriteLine("SetAccessor for " & Mypropertyinfob.Name & _
" returns a " & Mygetmethodinfob.ReturnType.ToString())
' Display the GetSetMethod without using the MethodInfo.
Console.WriteLine(MyTypea.FullName & "." & Mypropertyinfoa.Name & _
" GetSetMethod - " & Mypropertyinfoa.GetSetMethod().ToString())
Console.WriteLine(MyTypeb.FullName & "." & Mypropertyinfob.Name & _
" GetSetMethod - " & Mypropertyinfob.GetSetMethod().ToString())
Return 0
End Function
End Class
Remarques
Pour utiliser la GetSetMethod méthode, commencez par obtenir la classe Type. À partir de la Type, obtenir le PropertyInfo. À partir du PropertyInfo, utilisez la GetSetMethod méthode.
S’applique à
GetSetMethod()
Retourne l’accesseur public set pour cette propriété.
public:
virtual System::Reflection::MethodInfo ^ GetSetMethod();
public:
System::Reflection::MethodInfo ^ GetSetMethod();
public System.Reflection.MethodInfo GetSetMethod();
abstract member GetSetMethod : unit -> System.Reflection.MethodInfo
override this.GetSetMethod : unit -> System.Reflection.MethodInfo
member this.GetSetMethod : unit -> System.Reflection.MethodInfo
Public Function GetSetMethod () As MethodInfo
Retours
Objet MethodInfo représentant la Set méthode de cette propriété si l’accesseur set est public ou null si l’accesseur set n’est pas public.
Implémente
Remarques
Il s’agit d’une méthode pratique qui fournit une implémentation pour la méthode abstraite GetSetMethod avec le nonPublic paramètre défini sur false.
Pour utiliser la GetSetMethod méthode, commencez par obtenir la classe Type. À partir de la Type, obtenir le PropertyInfo. À partir du PropertyInfo, utilisez la GetSetMethod méthode.