Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

PropertyInfo.GetSetMethod Method (Boolean)

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
)
public abstract MethodInfo GetSetMethod (
	boolean nonPublic
)
public abstract function GetSetMethod (
	nonPublic : boolean
) : MethodInfo
Not applicable.

Parameters

nonPublic

Indicates whether the accessor should be returned if it is non-public. true if a non-public accessor is to be returned; otherwise, false.

Return Value

Value

Condition

A MethodInfo object representing the Set method for this property.

The set accessor is public.

-or-

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

a null reference (Nothing in Visual Basic)

nonPublic is true, but the property is read-only.

-or-

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

-or-

There is no set accessor.

Exception typeCondition

SecurityException

The requested method is non-public and the caller does not have ReflectionPermission to reflect on this non-public 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 displays the set accessor for the specified property.

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;
    }
}

import System.*;
import System.Reflection.*;

// Define a property.
public class MyProperty
{
    private String caption = "A Default caption";

    /** @property 
     */
    public String get_Caption()
    {
        return caption ;
    } //get_Caption

    /** @property 
     */
    public void set_Caption (String value)
    {
        if (caption != value) {
            caption = value;
        }
    } //set_Caption
} //MyProperty

class MyPropertyInfo
{   
    public static void main(String[] args)
    {
        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.get_Name() 
            + " returns a " + myGetmethodInfoA.get_ReturnType()));
        MethodInfo myGetmethodInfoB = myPropertyInfoB.GetSetMethod();
        Console.Write(("\nSetAccessor for " + myPropertyInfoB.get_Name() 
            + " returns a " + myGetmethodInfoB.get_ReturnType()));

        // Display the GetSetMethod without using the MethodInfo.
        Console.Write(("\n\n" + myTypeA.get_FullName() + "." 
            + myPropertyInfoA.get_Name() + " GetSetMethod - " 
            + myPropertyInfoA.GetSetMethod()));
        Console.Write(("\n" + myTypeB.get_FullName() + "." 
            + myPropertyInfoB.get_Name() + " GetSetMethod - " 
            + myPropertyInfoB.GetSetMethod()));
    } //main
} //MyPropertyInfo

import System;
import System.Reflection;
 
//Make a property
 public class Myproperty   
 {
    private var caption : String = "A Default caption";
    public function get Caption() : String {
        return caption;
    }
    public function set Caption(value:String) {
        if(caption!=value) caption = value;
    }
 }
 
 class Mypropertyinfo
 {
    public static function Main() : void
       {
       Console.WriteLine ("\nReflection.PropertyInfo");
 
       //Get the type and PropertyInfo for two separate properties
       var MyTypea : Type = Type.GetType("Myproperty");
       var Mypropertyinfoa : PropertyInfo = MyTypea.GetProperty("Caption");
       var MyTypeb : Type = Type.GetType("System.Text.StringBuilder");
       var Mypropertyinfob : PropertyInfo = MyTypeb.GetProperty("Length");
       //Get and display the GetSetMethod Method for each property
       var Mygetmethodinfoa : MethodInfo = Mypropertyinfoa.GetSetMethod();
       Console.Write ("\nSetAccessor for " + Mypropertyinfoa.Name
          + " returns a " + Mygetmethodinfoa.ReturnType);
       var Mygetmethodinfob : MethodInfo = 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());
    }
 }
 Mypropertyinfo.Main();
 /*
 Produces the following output
 
 Reflection.PropertyInfo
 SetAccessor for Caption returns a System.Void
 SetAccessor for Length returns a System.Void
 Myproperty.Caption GetSetMethod - Void set_Caption(System.String)
 System.Text.StringBuilder.Length GetSetMethod - Void set_Length(Int32)
 */

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

Show:
© 2014 Microsoft