[MediaContent] Fix description of Delete method (#866)
[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.Binding.Internals
8 {
9     internal static class ReflectionExtensions
10     {
11         public static FieldInfo GetField(this Type type, Func<FieldInfo, bool> predicate)
12         {
13             return GetFields(type).FirstOrDefault(predicate);
14         }
15
16         public static FieldInfo GetField(this Type type, string name)
17         {
18             return type.GetField(fi => fi.Name == name);
19         }
20
21         public static IEnumerable<FieldInfo> GetFields(this Type type)
22         {
23             return GetParts(type, i => i.DeclaredFields);
24         }
25
26         public static IEnumerable<PropertyInfo> GetProperties(this Type type)
27         {
28             return GetParts(type, ti => ti.DeclaredProperties);
29         }
30
31         public static PropertyInfo GetProperty(this Type type, string name)
32         {
33             Type t = type;
34             while (t != null)
35             {
36                 System.Reflection.TypeInfo ti = t.GetTypeInfo();
37                 PropertyInfo property = ti.GetDeclaredProperty(name);
38                 if (property != null)
39                     return property;
40
41                 t = ti.BaseType;
42             }
43
44             return null;
45         }
46
47         public static bool IsAssignableFrom(this Type self, Type c)
48         {
49             return self.GetTypeInfo().IsAssignableFrom(c.GetTypeInfo());
50         }
51
52         public static bool IsInstanceOfType(this Type self, object o)
53         {
54             return self.GetTypeInfo().IsAssignableFrom(o.GetType().GetTypeInfo());
55         }
56
57         static IEnumerable<T> GetParts<T>(Type type, Func<System.Reflection.TypeInfo, IEnumerable<T>> selector)
58         {
59             Type t = type;
60             while (t != null)
61             {
62                 System.Reflection.TypeInfo ti = t.GetTypeInfo();
63                 foreach (T f in selector(ti))
64                     yield return f;
65                 t = ti.BaseType;
66             }
67         }
68     }
69 }