Skip to content

Commit 9fc07e5

Browse files
committed
Added TypeInfo methods
1 parent dfc3afa commit 9fc07e5

File tree

1 file changed

+46
-65
lines changed

1 file changed

+46
-65
lines changed

src/Utility/IntrospectionExtensions.cs renamed to src/Utility/Compatibility.cs

Lines changed: 46 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
using System.Collections.Generic;
2-
using System.Threading;
2+
using System.Linq;
3+
using Unity.Attributes;
34

5+
#if NET40
46
namespace System.Reflection
57
{
6-
#if NET40
8+
using Unity;
9+
710
internal class TypeInfo
811
{
912
private const BindingFlags DeclaredOnlyLookup = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly;
10-
private Type _type;
13+
private readonly Type _type;
1114

1215

1316
internal TypeInfo(Type type)
@@ -46,6 +49,8 @@ internal TypeInfo(Type type)
4649

4750
public bool ContainsGenericParameters => _type.ContainsGenericParameters;
4851

52+
public bool IsConstructedGenericType => _type.IsGenericType && !_type.ContainsGenericParameters;
53+
4954
#region moved over from Type
5055

5156
//// Fields
@@ -72,17 +77,10 @@ public virtual IEnumerable<MethodInfo> GetDeclaredMethods(String name)
7277
}
7378
}
7479

75-
public virtual System.Reflection.TypeInfo GetDeclaredNestedType(String name)
80+
public virtual TypeInfo GetDeclaredNestedType(String name)
7681
{
7782
var nt = _type.GetNestedType(name, DeclaredOnlyLookup);
78-
if (nt == null)
79-
{
80-
return null; //the extension method GetTypeInfo throws for null
81-
}
82-
else
83-
{
84-
return nt.GetTypeInfo();
85-
}
83+
return nt == null ? null : nt.GetTypeInfo();
8684
}
8785

8886
public virtual PropertyInfo GetDeclaredProperty(String name)
@@ -93,46 +91,17 @@ public virtual PropertyInfo GetDeclaredProperty(String name)
9391

9492
//// Properties
9593

96-
public virtual IEnumerable<ConstructorInfo> DeclaredConstructors
97-
{
98-
get
99-
{
100-
return _type.GetConstructors(DeclaredOnlyLookup);
101-
}
102-
}
94+
public virtual IEnumerable<ConstructorInfo> DeclaredConstructors => _type.GetConstructors(DeclaredOnlyLookup);
10395

104-
public virtual IEnumerable<EventInfo> DeclaredEvents
105-
{
106-
get
107-
{
108-
return _type.GetEvents(DeclaredOnlyLookup);
109-
}
110-
}
96+
public virtual IEnumerable<EventInfo> DeclaredEvents => _type.GetEvents(DeclaredOnlyLookup);
11197

112-
public virtual IEnumerable<FieldInfo> DeclaredFields
113-
{
114-
get
115-
{
116-
return _type.GetFields(DeclaredOnlyLookup);
117-
}
118-
}
98+
public virtual IEnumerable<FieldInfo> DeclaredFields => _type.GetFields(DeclaredOnlyLookup);
11999

120-
public virtual IEnumerable<MemberInfo> DeclaredMembers
121-
{
122-
get
123-
{
124-
return _type.GetMembers(DeclaredOnlyLookup);
125-
}
126-
}
100+
public virtual IEnumerable<MemberInfo> DeclaredMembers => _type.GetMembers(DeclaredOnlyLookup);
127101

128-
public virtual IEnumerable<MethodInfo> DeclaredMethods
129-
{
130-
get
131-
{
132-
return _type.GetMethods(DeclaredOnlyLookup);
133-
}
134-
}
135-
public virtual IEnumerable<System.Reflection.TypeInfo> DeclaredNestedTypes
102+
public virtual IEnumerable<MethodInfo> DeclaredMethods => _type.GetMethods(DeclaredOnlyLookup);
103+
104+
public virtual IEnumerable<TypeInfo> DeclaredNestedTypes
136105
{
137106
get
138107
{
@@ -143,25 +112,12 @@ public virtual IEnumerable<System.Reflection.TypeInfo> DeclaredNestedTypes
143112
}
144113
}
145114

146-
public virtual IEnumerable<PropertyInfo> DeclaredProperties
147-
{
148-
get
149-
{
150-
return _type.GetProperties(DeclaredOnlyLookup);
151-
}
152-
}
153-
115+
public virtual IEnumerable<PropertyInfo> DeclaredProperties => _type.GetProperties(DeclaredOnlyLookup);
154116

155-
public virtual IEnumerable<Type> ImplementedInterfaces
156-
{
157-
get
158-
{
159-
return _type.GetInterfaces();
160-
}
161-
}
162117

118+
public virtual IEnumerable<Type> ImplementedInterfaces => _type.GetInterfaces();
163119

164-
#endregion
120+
#endregion
165121

166122
public override int GetHashCode()
167123
{
@@ -183,13 +139,38 @@ public override bool Equals(object obj)
183139
return left?.GetHashCode() != right?.GetHashCode();
184140
}
185141

142+
public Type GetGenericTypeDefinition()
143+
{
144+
return _type.GetGenericTypeDefinition();
145+
}
186146
}
147+
}
187148
#endif
188149

150+
namespace Unity
151+
{
152+
using System;
153+
using System.Reflection;
154+
189155

190-
internal static class IntrospectionExtensions
156+
internal static class Compatibility
191157
{
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+
192166
#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+
193174
public static TypeInfo GetTypeInfo(this Type type)
194175
{
195176
if (type == null)

0 commit comments

Comments
 (0)