Gets the property. - CSharp System.Reflection

CSharp examples for System.Reflection:PropertyInfo

Description

Gets the 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  a v  a 2  s  .c  o m

public class Main{
        /// <summary>
      /// Gets the property.
      /// </summary>
      /// <param name="type">The type.</param>
      /// <param name="name">The <see cref="string" /> containing the name of the public property to get.</param>
      /// <param name="returnType">Type of the return.</param>
      /// <param name="types">An array of <see cref="Type" /> objects representing the number, order, and type of
      /// the parameters for the indexed property to get.-or- An empty array of the type System.Type (that is,
      /// Type[] types = new Type[0]) to get a property that is not indexed.</param>
      /// <returns>
      /// A System.Reflection.PropertyInfo object representing the public property whose parameters match the specified argument types, if found; otherwise, null.
      /// </returns>
      public static PropertyInfo GetProperty( this Type type, string name, Type returnType, Type[] types )
      {
         return ( from property in type.GetRuntimeProperties()
                where property.Name == name
                where property.GetIndexParameters().Select( p => p.ParameterType ).SequenceEqual( types )
                select property ).SingleOrDefault();

      }
}

Related Tutorials