6 #if UNITY_WSA && !UNITY_EDITOR 9 using System.Collections.Generic;
13 public static class ReflectionExtensions
15 public static EventInfo GetEvent(
this Type type,
string eventName)
17 return type.GetRuntimeEvent(eventName);
20 public static MethodInfo GetMethod(
this Type type,
string methodName)
22 return GetMethod(type, methodName, (BindingFlags)0x0);
25 public static MethodInfo GetMethod(
this Type type,
string methodName, BindingFlags flags)
27 var result = type.GetTypeInfo().GetDeclaredMethod(methodName);
28 if (((flags & BindingFlags.FlattenHierarchy) != 0) && result == null)
30 var baseType = type.GetBaseType();
33 return GetMethod(baseType, methodName, flags);
40 public static MethodInfo GetMethod(
this Type type,
string methodName, BindingFlags bindingAttr,
Object binder, Type[] parameters,
Object[] modifiers)
42 var result = type.GetTypeInfo().GetDeclaredMethod(methodName);
45 var baseType = type.GetBaseType();
48 return GetMethod(baseType, methodName, bindingAttr, binder, parameters, modifiers);
55 public static MethodInfo GetMethod(
this Type type,
string methodName, Type[] parameters)
57 return GetMethods(type).Where(m => m.Name == methodName).FirstOrDefault(
60 var types = m.GetParameters().Select(p => p.ParameterType).ToArray();
61 if (types.Length == parameters.Length)
63 for (
int idx = 0; idx < types.Length; idx++)
65 if (types[idx] != parameters[idx])
81 public static IEnumerable<MethodInfo> GetMethods(
this Type type)
83 return GetMethods(type, (BindingFlags)0x0);
86 public static IEnumerable<MethodInfo> GetMethods(
this Type type, BindingFlags flags)
88 return type.GetTypeInfo().GetMethods(flags);
91 public static IEnumerable<MethodInfo> GetMethods(
this TypeInfo type)
93 return GetMethods(type, (BindingFlags)0x0);
96 public static IEnumerable<MethodInfo> GetMethods(
this TypeInfo type, BindingFlags flags)
98 return type.DeclaredMethods;
101 public static IEnumerable<FieldInfo> GetFields(
this Type type)
103 return GetFields(type, (BindingFlags)0x0);
106 public static IEnumerable<FieldInfo> GetFields(
this Type type, BindingFlags flags)
108 return type.GetTypeInfo().DeclaredFields;
111 public static FieldInfo GetField(
this Type type,
string fieldName)
113 return type.GetRuntimeField(fieldName);
116 public static IEnumerable<PropertyInfo> GetProperties(
this Type type, BindingFlags flags)
118 return type.GetTypeInfo().DeclaredProperties;
121 public static PropertyInfo GetProperty(
this Type type,
string propertyName)
123 return GetProperty(type, propertyName, (BindingFlags)0x0);
126 public static PropertyInfo GetProperty(
this Type type,
string propertyName, BindingFlags flags)
128 return type.GetRuntimeProperty(propertyName);
131 public static PropertyInfo GetProperty(
this Type type,
string propertyName, Type returnType)
133 return type.GetRuntimeProperty(propertyName);
136 public static IEnumerable<TypeInfo> GetTypes(
this Assembly assembly)
138 return assembly.DefinedTypes;
141 public static bool IsSubclassOf(
this Type type, Type c)
143 return type.GetTypeInfo().IsSubclassOf(c);
146 public static bool IsAssignableFrom(
this Type type, Type c)
148 return type.IsAssignableFrom(c.GetTypeInfo());
151 public static bool IsEnum(
this Type type)
153 return type.GetTypeInfo().IsEnum;
158 return type.GetTypeInfo().IsValueType;
161 public static bool IsAssignableFrom(
this Type type, TypeInfo typeInfo)
163 return type.GetTypeInfo().IsAssignableFrom(typeInfo);
166 public static object[] GetCustomAttributes(
this Type type,
bool inherit)
168 return type.GetTypeInfo().GetCustomAttributes(inherit).ToArray();
171 public static object[] GetCustomAttributes(
this Type type, Type attributeType,
bool inherit)
173 return type.GetTypeInfo().GetCustomAttributes(attributeType, inherit).ToArray();
187 public static Type
AsType(
this Type type)
192 public static bool IsEnum(
this Type type)
199 return type.IsValueType;