summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core/ReflectionExtensions.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Xamarin.Forms.Core/ReflectionExtensions.cs')
-rw-r--r--Xamarin.Forms.Core/ReflectionExtensions.cs68
1 files changed, 68 insertions, 0 deletions
diff --git a/Xamarin.Forms.Core/ReflectionExtensions.cs b/Xamarin.Forms.Core/ReflectionExtensions.cs
new file mode 100644
index 00000000..ab26fde5
--- /dev/null
+++ b/Xamarin.Forms.Core/ReflectionExtensions.cs
@@ -0,0 +1,68 @@
+´╗┐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<FieldInfo, bool> predicate)
+ {
+ return GetFields(type).SingleOrDefault(predicate);
+ }
+
+ public static FieldInfo GetField(this Type type, string name)
+ {
+ return type.GetField(fi => fi.Name == name);
+ }
+
+ public static IEnumerable<FieldInfo> GetFields(this Type type)
+ {
+ return GetParts(type, i => i.DeclaredFields);
+ }
+
+ public static IEnumerable<PropertyInfo> 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<T> GetParts<T>(Type type, Func<TypeInfo, IEnumerable<T>> selector)
+ {
+ Type t = type;
+ while (t != null)
+ {
+ TypeInfo ti = t.GetTypeInfo();
+ foreach (T f in selector(ti))
+ yield return f;
+ t = ti.BaseType;
+ }
+ }
+ }
+} \ No newline at end of file