-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
212 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,205 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Unity.Attributes; | ||
|
||
#if NET40 | ||
namespace System.Reflection | ||
{ | ||
using Unity; | ||
|
||
internal class TypeInfo | ||
{ | ||
private const BindingFlags DeclaredOnlyLookup = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly; | ||
private readonly Type _type; | ||
|
||
|
||
internal TypeInfo(Type type) | ||
{ | ||
_type = type ?? throw new ArgumentNullException(nameof(type)); | ||
} | ||
|
||
|
||
public Assembly Assembly => _type.Assembly; | ||
|
||
public bool IsGenericTypeDefinition => _type.IsGenericTypeDefinition; | ||
|
||
public Type[] GenericTypeArguments => _type.GetGenericArguments(); | ||
|
||
public Type[] GenericTypeParameters => _type.IsGenericTypeDefinition ? _type.GetGenericArguments() | ||
: Type.EmptyTypes; | ||
public string Name => _type.Name; | ||
|
||
public Type BaseType => _type.BaseType; | ||
|
||
public bool IsGenericType => _type.IsGenericType; | ||
|
||
public Type AsType() => _type; | ||
|
||
public bool IsAssignableFrom(TypeInfo typeInfo) => _type.IsAssignableFrom(typeInfo.AsType()); | ||
|
||
public bool IsGenericParameter => _type.IsGenericParameter; | ||
|
||
public bool IsInterface => _type.IsInterface; | ||
|
||
public bool IsAbstract => _type.IsAbstract; | ||
|
||
public bool IsSubclassOf(Type type) => _type.IsSubclassOf(type); | ||
|
||
public bool IsValueType => _type.IsValueType; | ||
|
||
public bool ContainsGenericParameters => _type.ContainsGenericParameters; | ||
|
||
public bool IsConstructedGenericType => _type.IsGenericType && !_type.ContainsGenericParameters; | ||
|
||
#region moved over from Type | ||
|
||
//// Fields | ||
|
||
public virtual EventInfo GetDeclaredEvent(String name) | ||
{ | ||
return _type.GetEvent(name, DeclaredOnlyLookup); | ||
} | ||
public virtual FieldInfo GetDeclaredField(String name) | ||
{ | ||
return _type.GetField(name, DeclaredOnlyLookup); | ||
} | ||
public virtual MethodInfo GetDeclaredMethod(String name) | ||
{ | ||
return _type.GetMethod(name, DeclaredOnlyLookup); | ||
} | ||
|
||
public virtual IEnumerable<MethodInfo> GetDeclaredMethods(String name) | ||
{ | ||
foreach (MethodInfo method in _type.GetMethods(DeclaredOnlyLookup)) | ||
{ | ||
if (method.Name == name) | ||
yield return method; | ||
} | ||
} | ||
|
||
public virtual TypeInfo GetDeclaredNestedType(String name) | ||
{ | ||
var nt = _type.GetNestedType(name, DeclaredOnlyLookup); | ||
return nt == null ? null : nt.GetTypeInfo(); | ||
} | ||
|
||
public virtual PropertyInfo GetDeclaredProperty(String name) | ||
{ | ||
return _type.GetProperty(name, DeclaredOnlyLookup); | ||
} | ||
|
||
|
||
//// Properties | ||
|
||
public virtual IEnumerable<ConstructorInfo> DeclaredConstructors => _type.GetConstructors(DeclaredOnlyLookup); | ||
|
||
public virtual IEnumerable<EventInfo> DeclaredEvents => _type.GetEvents(DeclaredOnlyLookup); | ||
|
||
public virtual IEnumerable<FieldInfo> DeclaredFields => _type.GetFields(DeclaredOnlyLookup); | ||
|
||
public virtual IEnumerable<MemberInfo> DeclaredMembers => _type.GetMembers(DeclaredOnlyLookup); | ||
|
||
public virtual IEnumerable<MethodInfo> DeclaredMethods => _type.GetMethods(DeclaredOnlyLookup); | ||
|
||
public virtual IEnumerable<TypeInfo> DeclaredNestedTypes | ||
{ | ||
get | ||
{ | ||
foreach (var t in _type.GetNestedTypes(DeclaredOnlyLookup)) | ||
{ | ||
yield return t.GetTypeInfo(); | ||
} | ||
} | ||
} | ||
|
||
public virtual IEnumerable<PropertyInfo> DeclaredProperties => _type.GetProperties(DeclaredOnlyLookup); | ||
|
||
|
||
public virtual IEnumerable<Type> ImplementedInterfaces => _type.GetInterfaces(); | ||
|
||
#endregion | ||
|
||
public override int GetHashCode() | ||
{ | ||
return _type.GetHashCode(); | ||
} | ||
|
||
public override bool Equals(object obj) | ||
{ | ||
return _type.Equals(obj); | ||
} | ||
|
||
public static bool operator ==(TypeInfo left, TypeInfo right) | ||
{ | ||
return left?.GetHashCode() == right?.GetHashCode(); | ||
} | ||
|
||
public static bool operator !=(TypeInfo left, TypeInfo right) | ||
{ | ||
return left?.GetHashCode() != right?.GetHashCode(); | ||
} | ||
|
||
public Type GetGenericTypeDefinition() | ||
{ | ||
return _type.GetGenericTypeDefinition(); | ||
} | ||
} | ||
} | ||
#endif | ||
|
||
namespace Unity | ||
{ | ||
using System; | ||
using System.Reflection; | ||
|
||
|
||
internal static class Compatibility | ||
{ | ||
#if NETSTANDARD1_0 | ||
public static System.Reflection.ConstructorInfo[] GetConstructors(this System.Type type) | ||
{ | ||
var ctors = type.GetTypeInfo().DeclaredConstructors; | ||
return ctors is ConstructorInfo[] array ? array : ctors.ToArray(); | ||
} | ||
#endif | ||
|
||
#if NET40 | ||
public static Attribute GetCustomAttribute(this ParameterInfo parameter, Type type) | ||
{ | ||
return parameter.GetCustomAttributes(false) | ||
.OfType<DependencyResolutionAttribute>() | ||
.FirstOrDefault(); | ||
} | ||
|
||
public static TypeInfo GetTypeInfo(this Type type) | ||
{ | ||
if (type == null) | ||
{ | ||
throw new ArgumentNullException(nameof(type)); | ||
} | ||
|
||
return new TypeInfo(type); | ||
} | ||
|
||
public static Delegate CreateDelegate(this MethodInfo method, Type delegateType) | ||
{ | ||
return Delegate.CreateDelegate(delegateType, method); | ||
} | ||
|
||
public static Delegate CreateDelegate(this MethodInfo method, Type delegateType, object target) | ||
{ | ||
return Delegate.CreateDelegate(delegateType, target, method); | ||
} | ||
#else | ||
public static MethodInfo GetGetMethod(this PropertyInfo info, bool _) | ||
{ | ||
return info.GetMethod; | ||
} | ||
|
||
public static MethodInfo GetSetMethod(this PropertyInfo info, bool _) | ||
{ | ||
return info.SetMethod; | ||
} | ||
#endif | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters