PropertyInfo.GetSetMethod Method

Definition

Returns a MethodInfo representing the set accessor for this property.

Overloads

GetSetMethod(Boolean)

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

GetSetMethod()

Returns the public set accessor for this property.

GetSetMethod(Boolean)

Source:
PropertyInfo.cs
Source:
PropertyInfo.cs
Source:
PropertyInfo.cs

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

C#
public abstract System.Reflection.MethodInfo? GetSetMethod(bool nonPublic);
C#
public abstract System.Reflection.MethodInfo GetSetMethod(bool nonPublic);

Parameters

nonPublic
Boolean

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

Returns

This property's Set method, or null, as shown in the following table.

Value Condition
The Set method for this property. The set accessor is public, OR nonPublic is true and the set accessor is non-public.
nullnonPublic 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.

Implements

Exceptions

The requested method is non-public and the caller does not have ReflectionPermission to reflect on this non-public method.

Examples

The following example displays the set accessor for the specified property.

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

Remarks

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

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.5, 1.6, 2.0, 2.1

GetSetMethod()

Source:
PropertyInfo.cs
Source:
PropertyInfo.cs
Source:
PropertyInfo.cs

Returns the public set accessor for this property.

C#
public System.Reflection.MethodInfo? GetSetMethod();
C#
public System.Reflection.MethodInfo GetSetMethod();

Returns

The MethodInfo object representing the Set method for this property if the set accessor is public, or null if the set accessor is not public.

Implements

Remarks

This is a convenience method that provides an implementation for the abstract GetSetMethod method 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.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.5, 1.6, 2.0, 2.1