[NUI]Add xaml support for nui and nui xaml test sample (#230)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / Xaml / ReflectionExtensions.cs
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Linq;
5 using System.Reflection;
6
7 namespace Tizen.NUI.Internals
8 {
9         [EditorBrowsable(EditorBrowsableState.Never)]
10         internal static class ReflectionExtensions
11         {
12                 public static FieldInfo GetField(this Type type, Func<FieldInfo, bool> predicate)
13                 {
14                         return GetFields(type).FirstOrDefault(predicate);
15                 }
16
17                 public static FieldInfo GetField(this Type type, string name)
18                 {
19                         return type.GetField(fi => fi.Name == name);
20                 }
21
22                 public static IEnumerable<FieldInfo> GetFields(this Type type)
23                 {
24                         return GetParts(type, i => i.DeclaredFields);
25                 }
26
27                 public static IEnumerable<PropertyInfo> GetProperties(this Type type)
28                 {
29                         return GetParts(type, ti => ti.DeclaredProperties);
30                 }
31
32                 public static PropertyInfo GetProperty(this Type type, string name)
33                 {
34                         Type t = type;
35                         while (t != null)
36                         {
37                                 System.Reflection.TypeInfo ti = t.GetTypeInfo();
38                                 PropertyInfo property = ti.GetDeclaredProperty(name);
39                                 if (property != null)
40                                         return property;
41
42                                 t = ti.BaseType;
43                         }
44
45                         return null;
46                 }
47
48                 public static bool IsAssignableFrom(this Type self, Type c)
49                 {
50                         return self.GetTypeInfo().IsAssignableFrom(c.GetTypeInfo());
51                 }
52
53                 public static bool IsInstanceOfType(this Type self, object o)
54                 {
55                         return self.GetTypeInfo().IsAssignableFrom(o.GetType().GetTypeInfo());
56                 }
57
58                 static IEnumerable<T> GetParts<T>(Type type, Func<System.Reflection.TypeInfo, IEnumerable<T>> selector)
59                 {
60                         Type t = type;
61                         while (t != null)
62                         {
63                                 System.Reflection.TypeInfo ti = t.GetTypeInfo();
64                                 foreach (T f in selector(ti))
65                                         yield return f;
66                                 t = ti.BaseType;
67                         }
68                 }
69         }
70 }