PropertyInfo.GetSetMethod Method (Boolean)

July 28, 2014

When overridden in a derived class, returns the set accessor for this property.

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

public abstract MethodInfo GetSetMethod(
	bool nonPublic
)

Parameters

nonPublic
Type: System.Boolean
true to return a non-public accessor; otherwise, false.

Return Value

Type: System.Reflection.MethodInfo

Value

Condition

The set accessor for this property.

The set accessor is public.

-or-

nonPublic is true and a set accessor exists.

null

nonPublic is false and the set accessor is non-public.

-or-

There is no set accessor because the property is read-only.

ExceptionCondition
MethodAccessException

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

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

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.


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)
 */


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2014 Microsoft