1+ using System . Collections . Generic ;
2+ using System . Linq ;
3+ using Unity . Attributes ;
4+
5+ #if NET40
6+ namespace System . Reflection
7+ {
8+ using Unity ;
9+
10+ internal class TypeInfo
11+ {
12+ private const BindingFlags DeclaredOnlyLookup = BindingFlags . Public | BindingFlags . NonPublic | BindingFlags . Instance | BindingFlags . Static | BindingFlags . DeclaredOnly ;
13+ private readonly Type _type ;
14+
15+
16+ internal TypeInfo ( Type type )
17+ {
18+ _type = type ?? throw new ArgumentNullException ( nameof ( type ) ) ;
19+ }
20+
21+
22+ public Assembly Assembly => _type . Assembly ;
23+
24+ public bool IsGenericTypeDefinition => _type . IsGenericTypeDefinition ;
25+
26+ public Type [ ] GenericTypeArguments => _type . GetGenericArguments ( ) ;
27+
28+ public Type [ ] GenericTypeParameters => _type . IsGenericTypeDefinition ? _type . GetGenericArguments ( )
29+ : Type . EmptyTypes ;
30+ public string Name => _type . Name ;
31+
32+ public Type BaseType => _type . BaseType ;
33+
34+ public bool IsGenericType => _type . IsGenericType ;
35+
36+ public Type AsType ( ) => _type ;
37+
38+ public bool IsAssignableFrom ( TypeInfo typeInfo ) => _type . IsAssignableFrom ( typeInfo . AsType ( ) ) ;
39+
40+ public bool IsGenericParameter => _type . IsGenericParameter ;
41+
42+ public bool IsInterface => _type . IsInterface ;
43+
44+ public bool IsAbstract => _type . IsAbstract ;
45+
46+ public bool IsSubclassOf ( Type type ) => _type . IsSubclassOf ( type ) ;
47+
48+ public bool IsValueType => _type . IsValueType ;
49+
50+ public bool ContainsGenericParameters => _type . ContainsGenericParameters ;
51+
52+ public bool IsConstructedGenericType => _type . IsGenericType && ! _type . ContainsGenericParameters ;
53+
54+ #region moved over from Type
55+
56+ //// Fields
57+
58+ public virtual EventInfo GetDeclaredEvent ( String name )
59+ {
60+ return _type . GetEvent ( name , DeclaredOnlyLookup ) ;
61+ }
62+ public virtual FieldInfo GetDeclaredField ( String name )
63+ {
64+ return _type . GetField ( name , DeclaredOnlyLookup ) ;
65+ }
66+ public virtual MethodInfo GetDeclaredMethod ( String name )
67+ {
68+ return _type . GetMethod ( name , DeclaredOnlyLookup ) ;
69+ }
70+
71+ public virtual IEnumerable < MethodInfo > GetDeclaredMethods ( String name )
72+ {
73+ foreach ( MethodInfo method in _type . GetMethods ( DeclaredOnlyLookup ) )
74+ {
75+ if ( method . Name == name )
76+ yield return method ;
77+ }
78+ }
79+
80+ public virtual TypeInfo GetDeclaredNestedType ( String name )
81+ {
82+ var nt = _type . GetNestedType ( name , DeclaredOnlyLookup ) ;
83+ return nt == null ? null : nt . GetTypeInfo ( ) ;
84+ }
85+
86+ public virtual PropertyInfo GetDeclaredProperty ( String name )
87+ {
88+ return _type . GetProperty ( name , DeclaredOnlyLookup ) ;
89+ }
90+
91+
92+ //// Properties
93+
94+ public virtual IEnumerable < ConstructorInfo > DeclaredConstructors => _type . GetConstructors ( DeclaredOnlyLookup ) ;
95+
96+ public virtual IEnumerable < EventInfo > DeclaredEvents => _type . GetEvents ( DeclaredOnlyLookup ) ;
97+
98+ public virtual IEnumerable < FieldInfo > DeclaredFields => _type . GetFields ( DeclaredOnlyLookup ) ;
99+
100+ public virtual IEnumerable < MemberInfo > DeclaredMembers => _type . GetMembers ( DeclaredOnlyLookup ) ;
101+
102+ public virtual IEnumerable < MethodInfo > DeclaredMethods => _type . GetMethods ( DeclaredOnlyLookup ) ;
103+
104+ public virtual IEnumerable < TypeInfo > DeclaredNestedTypes
105+ {
106+ get
107+ {
108+ foreach ( var t in _type . GetNestedTypes ( DeclaredOnlyLookup ) )
109+ {
110+ yield return t . GetTypeInfo ( ) ;
111+ }
112+ }
113+ }
114+
115+ public virtual IEnumerable < PropertyInfo > DeclaredProperties => _type . GetProperties ( DeclaredOnlyLookup ) ;
116+
117+
118+ public virtual IEnumerable < Type > ImplementedInterfaces => _type . GetInterfaces ( ) ;
119+
120+ #endregion
121+
122+ public override int GetHashCode ( )
123+ {
124+ return _type . GetHashCode ( ) ;
125+ }
126+
127+ public override bool Equals ( object obj )
128+ {
129+ return _type . Equals ( obj ) ;
130+ }
131+
132+ public static bool operator == ( TypeInfo left , TypeInfo right )
133+ {
134+ return left ? . GetHashCode ( ) == right ? . GetHashCode ( ) ;
135+ }
136+
137+ public static bool operator != ( TypeInfo left , TypeInfo right )
138+ {
139+ return left ? . GetHashCode ( ) != right ? . GetHashCode ( ) ;
140+ }
141+
142+ public Type GetGenericTypeDefinition ( )
143+ {
144+ return _type . GetGenericTypeDefinition ( ) ;
145+ }
146+ }
147+ }
148+ #endif
149+
150+ namespace Unity
151+ {
152+ using System ;
153+ using System . Reflection ;
154+
155+
156+ internal static class Compatibility
157+ {
158+ #if NETSTANDARD1_0
159+ public static System . Reflection . ConstructorInfo [ ] GetConstructors ( this System . Type type )
160+ {
161+ var ctors = type . GetTypeInfo ( ) . DeclaredConstructors ;
162+ return ctors is ConstructorInfo [ ] array ? array : ctors . ToArray ( ) ;
163+ }
164+ #endif
165+
166+ #if NET40
167+ public static Attribute GetCustomAttribute ( this ParameterInfo parameter , Type type )
168+ {
169+ return parameter . GetCustomAttributes ( false )
170+ . OfType < DependencyResolutionAttribute > ( )
171+ . FirstOrDefault ( ) ;
172+ }
173+
174+ public static TypeInfo GetTypeInfo ( this Type type )
175+ {
176+ if ( type == null )
177+ {
178+ throw new ArgumentNullException ( nameof ( type ) ) ;
179+ }
180+
181+ return new TypeInfo ( type ) ;
182+ }
183+
184+ public static Delegate CreateDelegate ( this MethodInfo method , Type delegateType )
185+ {
186+ return Delegate . CreateDelegate ( delegateType , method ) ;
187+ }
188+
189+ public static Delegate CreateDelegate ( this MethodInfo method , Type delegateType , object target )
190+ {
191+ return Delegate . CreateDelegate ( delegateType , target , method ) ;
192+ }
193+ #else
194+ public static MethodInfo GetGetMethod ( this PropertyInfo info , bool _ )
195+ {
196+ return info . GetMethod ;
197+ }
198+
199+ public static MethodInfo GetSetMethod ( this PropertyInfo info , bool _ )
200+ {
201+ return info . SetMethod ;
202+ }
203+ #endif
204+ }
205+ }
0 commit comments