Returns the set accessor for this property. - CSharp System.Reflection

CSharp examples for System.Reflection:PropertyInfo

Description

Returns the set accessor for this property.

Demo Code

// See LICENSE.txt for details or visit http://www.opensource.org/licenses/ms-pl.html
using System.Reflection;
using System.Linq;
using System;/*  w  w  w  .  jav a2  s.co m*/

public class Main{
        /// <summary>
      /// Returns the set accessor for this property.
      /// </summary>
      /// <param name="property">The property.</param>
      /// <param name="isPrivate">Indicates whether the accessor should be returned if it is non-public. true if a non-public accessor is to be returned; otherwise, false.</param>
      /// <returns></returns>
      public static MethodInfo GetSetMethod( this PropertyInfo property, bool isPrivate )
      {
         if( !isPrivate && property.SetMethod.IsPrivate )
         {
            return null;
         }

         return property.SetMethod;
      }
        /// <summary>
      /// Returns the public set accessor for this property.
      /// </summary>
      /// <param name="property">The property.</param>
      /// <returns>A MethodInfo object representing the public set accessor for this property, or null if the set accessor is non-public or does not exist.</returns>
      public static MethodInfo GetSetMethod( this PropertyInfo property )
      {
         return property.SetMethod.IsPublic ? property.SetMethod : null;
      }
}

Related Tutorials