Find Field - CSharp System.Reflection

CSharp examples for System.Reflection:FieldInfo

Description

Find Field

Demo Code


using System.Reflection;
using System.Linq;
using System.Collections.Generic;
using System;//  www . j av  a 2 s . c o  m

public class Main{

        public static FieldInfo FindField(this Type type, string fieldName, BindingFlags bindingAttr = BindingFlags.Instance | BindingFlags.NonPublic)
        {
            if (!string.IsNullOrWhiteSpace(fieldName))
            {
                var field = type.GetField(fieldName, bindingAttr);
                if (field != null)
                {
                    return field;
                }
                else if (type.BaseType != null)
                {
                    return type.BaseType.FindField(fieldName, bindingAttr);
                }
            }
            return null;
        }
}

Related Tutorials