Finds the property recursive. - CSharp System.Reflection

CSharp examples for System.Reflection:PropertyInfo

Description

Finds the property recursive.

Demo Code

// Copyright (c) Daniel Dabrowski - rod.blogsome.com.  All rights reserved.
using global::System.Reflection;
using global::System;

public class Main{
        /// <summary>
      /// Finds the property recursive.
      /// </summary>
      /// <param name="type">The class type.</param>
      /// <param name="name">The property name.</param>
      /// <returns>PropertyInfo or null if there is no field with such name.</returns>
      public static PropertyInfo FindProperty(Type type, string name)
      {/*from   ww  w  . j  a va2s  .c o  m*/
         if (type == null || type == typeof(object))
         {
            // the full inheritance chain has been walked and we could
            // not find the Field
            return null;
         }

         return type.GetProperty(name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly) ??
               FindProperty(type.BaseType, name);
      }
}

Related Tutorials