Locate Field - CSharp System.Reflection

CSharp examples for System.Reflection:FieldInfo

Description

Locate Field

Demo Code

// The MIT License (MIT)
using System.Reflection;
using System;/*from www .jav  a  2  s .co m*/

public class Main{
        private static FieldInfo LocateField(this Type targetType, string name)
        {
            try
            {
                FieldInfo fieldInfo = targetType.GetField(name, ALL_PRIVATE_FLAGS);
                if (fieldInfo == null && targetType.BaseType != typeof(object))
                    return targetType.BaseType.LocateField(name);
                return fieldInfo;
            }
            catch (ArgumentException)
            {
                if (targetType.BaseType != typeof(object))
                {
                    return targetType.BaseType.LocateField(name);
                }
                throw;
            }
        }
}

Related Tutorials