Finds the field recursive. - CSharp System.Reflection

CSharp examples for System.Reflection:FieldInfo

Description

Finds the field recursive.

Demo Code


using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using System;/*  w  w  w .ja  va 2 s  . c om*/

public class Main{
        /// <summary>
        /// Finds the field recursive.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public static FieldInfo FindField(Type type, string name)
        {
            if (type == null || type == typeof(object))
            {
                // the full inheritance chain has been walked and we could
                // not find the Field
                return null;
            }

            FieldInfo field =
                type.GetField(name,
                              BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly) ??
                FindField(type.BaseType, name);

            return field;
        }
}

Related Tutorials