AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
ReflectionExtensions.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 namespace HoloToolkit
5 {
6 #if UNITY_WSA && !UNITY_EDITOR
7 
8  using System;
9  using System.Collections.Generic;
10  using System.Linq;
11  using System.Reflection;
12 
13  public static class ReflectionExtensions
14  {
15  public static EventInfo GetEvent(this Type type, string eventName)
16  {
17  return type.GetRuntimeEvent(eventName);
18  }
19 
20  public static MethodInfo GetMethod(this Type type, string methodName)
21  {
22  return GetMethod(type, methodName, (BindingFlags)0x0);
23  }
24 
25  public static MethodInfo GetMethod(this Type type, string methodName, BindingFlags flags)
26  {
27  var result = type.GetTypeInfo().GetDeclaredMethod(methodName);
28  if (((flags & BindingFlags.FlattenHierarchy) != 0) && result == null)
29  {
30  var baseType = type.GetBaseType();
31  if (baseType != null)
32  {
33  return GetMethod(baseType, methodName, flags);
34  }
35  }
36 
37  return result;
38  }
39 
40  public static MethodInfo GetMethod(this Type type, string methodName, BindingFlags bindingAttr, Object binder, Type[] parameters, Object[] modifiers)
41  {
42  var result = type.GetTypeInfo().GetDeclaredMethod(methodName);
43  if (result == null)
44  {
45  var baseType = type.GetBaseType();
46  if (baseType != null)
47  {
48  return GetMethod(baseType, methodName, bindingAttr, binder, parameters, modifiers);
49  }
50  }
51 
52  return result;
53  }
54 
55  public static MethodInfo GetMethod(this Type type, string methodName, Type[] parameters)
56  {
57  return GetMethods(type).Where(m => m.Name == methodName).FirstOrDefault(
58  m =>
59  {
60  var types = m.GetParameters().Select(p => p.ParameterType).ToArray();
61  if (types.Length == parameters.Length)
62  {
63  for (int idx = 0; idx < types.Length; idx++)
64  {
65  if (types[idx] != parameters[idx])
66  {
67  return false;
68  }
69  }
70 
71  return true;
72  }
73  else
74  {
75  return false;
76  }
77  }
78  );
79  }
80 
81  public static IEnumerable<MethodInfo> GetMethods(this Type type)
82  {
83  return GetMethods(type, (BindingFlags)0x0);
84  }
85 
86  public static IEnumerable<MethodInfo> GetMethods(this Type type, BindingFlags flags)
87  {
88  return type.GetTypeInfo().GetMethods(flags);
89  }
90 
91  public static IEnumerable<MethodInfo> GetMethods(this TypeInfo type)
92  {
93  return GetMethods(type, (BindingFlags)0x0);
94  }
95 
96  public static IEnumerable<MethodInfo> GetMethods(this TypeInfo type, BindingFlags flags)
97  {
98  return type.DeclaredMethods;
99  }
100 
101  public static IEnumerable<FieldInfo> GetFields(this Type type)
102  {
103  return GetFields(type, (BindingFlags)0x0);
104  }
105 
106  public static IEnumerable<FieldInfo> GetFields(this Type type, BindingFlags flags)
107  {
108  return type.GetTypeInfo().DeclaredFields;
109  }
110 
111  public static FieldInfo GetField(this Type type, string fieldName)
112  {
113  return type.GetRuntimeField(fieldName);
114  }
115 
116  public static IEnumerable<PropertyInfo> GetProperties(this Type type, BindingFlags flags)
117  {
118  return type.GetTypeInfo().DeclaredProperties;
119  }
120 
121  public static PropertyInfo GetProperty(this Type type, string propertyName)
122  {
123  return GetProperty(type, propertyName, (BindingFlags)0x0);
124  }
125 
126  public static PropertyInfo GetProperty(this Type type, string propertyName, BindingFlags flags)
127  {
128  return type.GetRuntimeProperty(propertyName);
129  }
130 
131  public static PropertyInfo GetProperty(this Type type, string propertyName, Type returnType)
132  {
133  return type.GetRuntimeProperty(propertyName);
134  }
135 
136  public static IEnumerable<TypeInfo> GetTypes(this Assembly assembly)
137  {
138  return assembly.DefinedTypes;
139  }
140 
141  public static bool IsSubclassOf(this Type type, Type c)
142  {
143  return type.GetTypeInfo().IsSubclassOf(c);
144  }
145 
146  public static bool IsAssignableFrom(this Type type, Type c)
147  {
148  return type.IsAssignableFrom(c.GetTypeInfo());
149  }
150 
151  public static bool IsEnum(this Type type)
152  {
153  return type.GetTypeInfo().IsEnum;
154  }
155 
156  public static bool IsValueType(this Type type)
157  {
158  return type.GetTypeInfo().IsValueType;
159  }
160 
161  public static bool IsAssignableFrom(this Type type, TypeInfo typeInfo)
162  {
163  return type.GetTypeInfo().IsAssignableFrom(typeInfo);
164  }
165 
166  public static object[] GetCustomAttributes(this Type type, bool inherit)
167  {
168  return type.GetTypeInfo().GetCustomAttributes(inherit).ToArray();
169  }
170 
171  public static object[] GetCustomAttributes(this Type type, Type attributeType, bool inherit)
172  {
173  return type.GetTypeInfo().GetCustomAttributes(attributeType, inherit).ToArray();
174  }
175  }
176 #else
177 
178  using System;
179 
180  public static class ReflectionExtensions
181  {
182  public static Type GetTypeInfo(this Type type)
183  {
184  return type;
185  }
186 
187  public static Type AsType(this Type type)
188  {
189  return type;
190  }
191 
192  public static bool IsEnum(this Type type)
193  {
194  return type.IsEnum;
195  }
196 
197  public static bool IsValueType(this Type type)
198  {
199  return type.IsValueType;
200  }
201  }
202 
203 #endif
204 }
static Type AsType(this Type type)
static Type GetTypeInfo(this Type type)
static bool IsEnum(this Type type)
static bool IsValueType(this Type type)