Returns the public or non-public get accessor for this property. - CSharp System.Reflection

CSharp examples for System.Reflection:PropertyInfo

Description

Returns the public or non-public get 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;/*from  w  w w . j  av  a  2  s .c  om*/

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

         return property.GetMethod;
      }
        /// <summary>
      /// Returns the public get accessor for this property.
      /// </summary>
      /// <param name="property">The property.</param>
      /// <returns>A MethodInfo object representing the public get accessor for this property, or null if the get accessor is non-public or does not exist.</returns>
      public static MethodInfo GetGetMethod( this PropertyInfo property )
      {
         return property.GetMethod.IsPublic ? property.GetMethod : null;
      }
        /// <summary>
      /// Searches for the specified public method whose parameters match the specified argument types.
      /// </summary>
      /// <param name="type">The type.</param>
      /// <param name="name">The System.String containing the name of the public method to get.</param>
      /// <param name="types">An array of System.Type objects representing the number, order, and type of the parameters for the method to get.-or- An empty array of System.Type objects (as provided by the System.Type.EmptyTypes field) to get a method that takes no parameters.</param>
      /// <returns></returns>
      public static MethodInfo GetMethod( this Type type, string name, Type[] types )
      {
         return ( from method in type.GetRuntimeMethods()
                  where method.Name == name
                where method.GetParameters().Select( p => p.ParameterType ).SequenceEqual( types )
                  select method ).SingleOrDefault();
      }
        /// <summary>
      /// Searches for the public method with the specified name.
      /// </summary>
      /// <param name="type">The type.</param>
      /// <param name="name">The System.String containing the name of the public method to get.</param>
      /// <returns>A System.Reflection.MethodInfo object representing the public method with the specified name, if found; otherwise, null.</returns>
      public static MethodInfo GetMethod( this Type type, string name )
      {
         return ( from method in type.GetRuntimeMethods()
                  where method.Name == name
                select method ).SingleOrDefault();
      }
}

Related Tutorials