using System; using System.Collections.Generic; using System.Linq; using System.Reflection; namespace Xamarin.Forms { internal static class ReflectionExtensions { public static FieldInfo GetField(this Type type, Func predicate) { return GetFields(type).FirstOrDefault(predicate); } public static FieldInfo GetField(this Type type, string name) { return type.GetField(fi => fi.Name == name); } public static IEnumerable GetFields(this Type type) { return GetParts(type, i => i.DeclaredFields); } public static IEnumerable GetProperties(this Type type) { return GetParts(type, ti => ti.DeclaredProperties); } public static PropertyInfo GetProperty(this Type type, string name) { Type t = type; while (t != null) { TypeInfo ti = t.GetTypeInfo(); PropertyInfo property = ti.GetDeclaredProperty(name); if (property != null) return property; t = ti.BaseType; } return null; } public static bool IsAssignableFrom(this Type self, Type c) { return self.GetTypeInfo().IsAssignableFrom(c.GetTypeInfo()); } public static bool IsInstanceOfType(this Type self, object o) { return self.GetTypeInfo().IsAssignableFrom(o.GetType().GetTypeInfo()); } static IEnumerable GetParts(Type type, Func> selector) { Type t = type; while (t != null) { TypeInfo ti = t.GetTypeInfo(); foreach (T f in selector(ti)) yield return f; t = ti.BaseType; } } } }