PropertyInfo.GetSetMethod Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Returns the public set accessor for this property.

Namespace:  System.Reflection
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Function GetSetMethod As MethodInfo
public MethodInfo GetSetMethod()

Return Value

Type: System.Reflection.MethodInfo
The set accessor for this property, if the set accessor exists and is public; otherwise, nulla null reference (Nothing in Visual Basic).

Exceptions

Exception Condition
MethodAccessException

Application code attempts to access this member late-bound, for example by using the Type.InvokeMember method.

Remarks

This is a convenience method that is equivalent to calling the GetSetMethod(Boolean) method overload with the nonPublic parameter set to false.

To use the GetSetMethod method, first get the class Type. From the Type, get the PropertyInfo. From the PropertyInfo, use the GetSetMethod method.

Examples

The following example demonstrates both overloads of the GetSetMethod method. The example defines a public property and a protected property. The example uses the GetGetMethod() and GetGetMethod(Boolean) method overloads to display the set accessors of both properties. In the first case, only the set accessor for the public property is displayed.

Imports System.Reflection

Class Example

   ' Define properties with different access levels.

   Private myCaption As String = "A Default caption"
   Public Property Caption() As String
      Get
         Return myCaption
      End Get
      Set
         myCaption = Value
      End Set
   End Property

   Private myText As String = "Default text."
   Protected Property Text() As String
      Get
         Return myText
      End Get
      Set
         myText = Value
      End Set
   End Property


   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock) 

      ' Get the PropertyInfo objects.
      Dim captionInfo As PropertyInfo = GetType(Example).GetProperty("Caption")
      Dim textInfo As PropertyInfo = _
         GetType(Example).GetProperty("Text", _
                                      BindingFlags.NonPublic Or BindingFlags.Instance)

      outputBlock.Text &= "Public set accessors:" & vbLf

      ' List the public set accessors.
      Dim publicSetAccessors() As MethodInfo = { captionInfo.GetSetMethod(), _
                                                 textInfo.GetSetMethod() }

      For Each mi As MethodInfo In publicSetAccessors
         If mi Is Nothing Then
            outputBlock.Text &= "No set accessor was found." & vbLf
         Else
            outputBlock.Text &= mi.ToString() & vbLf
         End If
      Next

      outputBlock.Text &= vbLf & "All set accessors:" & vbLf

      ' List all set accessors.
      Dim allSetAccessors() As MethodInfo = { captionInfo.GetSetMethod(True), _
                                              textInfo.GetSetMethod(True) }

      For Each mi As MethodInfo In allSetAccessors
         If mi Is Nothing Then
            outputBlock.Text &= "No set accessor was found." & vbLf
         Else
            outputBlock.Text &= mi.ToString() & vbLf
         End If
      Next

   End Sub
End Class

' This example produces the following output:
'
'Public set accessors:
'Void set_Caption(System.String)
'No set accessor was found.
'
'All set accessors:
'Void set_Caption(System.String)
'Void set_Text(System.String)
using System;
using System.Reflection;

class Example
{
   // Define properties with different access levels.

   private string myCaption = "A Default caption";
   public string Caption   
   {
      get
      {
         return myCaption;
      }
      set
      {
         myCaption = value;
      }
   }

   private string myText = "Default text.";
   protected string Text   
   {
      get
      {
         return myText;
      }
      set
      {
         myText = value;
      }
   }

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      // Get the PropertyInfo objects.
      PropertyInfo captionInfo = typeof(Example).GetProperty("Caption");
      PropertyInfo textInfo = 
         typeof(Example).GetProperty("Text", 
                                     BindingFlags.NonPublic | BindingFlags.Instance);

      outputBlock.Text += "Public set accessors:\n";

      // List the public set accessors.
      MethodInfo[] publicSetAccessors = { captionInfo.GetSetMethod(), 
                                          textInfo.GetSetMethod() };

      foreach (MethodInfo mi in publicSetAccessors)
      {
         if (mi == null)
         {
            outputBlock.Text += "No set accessor was found.\n";
         }
         else
         {
            outputBlock.Text += mi.ToString() + "\n";
         }
      }

      outputBlock.Text += "\nAll set accessors:\n";

      // List all set accessors.
      MethodInfo[] allSetAccessors = { captionInfo.GetSetMethod(true), 
                                       textInfo.GetSetMethod(true) };

      foreach( MethodInfo mi in allSetAccessors )
      {
         if (mi==null)
         {
            outputBlock.Text += "No set accessor was found.\n";
         }
         else
         {
            outputBlock.Text += mi.ToString() + "\n";
         }
      }
   }
}

/* This example produces the following output:

Public set accessors:
Void set_Caption(System.String)
No set accessor was found.

All set accessors:
Void set_Caption(System.String)
Void set_Text(System.String)
 */

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.