diff --git a/src/Properties/Resources.Designer.cs b/src/Properties/Resources.Designer.cs index c8dc564..550feff 100644 --- a/src/Properties/Resources.Designer.cs +++ b/src/Properties/Resources.Designer.cs @@ -10,7 +10,6 @@ namespace Unity.Interception.Properties { using System; - using System.Reflection; /// diff --git a/src/Unity.Interception.csproj b/src/Unity.Interception.csproj index 2743fd2..b493be8 100644 --- a/src/Unity.Interception.csproj +++ b/src/Unity.Interception.csproj @@ -1,8 +1,6 @@  - net47 - false Unity Interception 5.0.0-beta 5.0.0.0 @@ -17,28 +15,9 @@ Unity.Interception Microsoft.Practices.Unity Microsoft.Practices.Unity + Debug;Release;Package - - true - ..\..\package.snk - false - - - - Full - - - - - - - - - - - - True @@ -54,5 +33,43 @@ + + true + package.snk + false + + + + + + + + + + + + true + net47;net45;net40 + + + + false + net47;net45;net40 + + + + Full + false + net47 + + + + + + + + + + diff --git a/src/Utilities/IntrospectionExtensions.cs b/src/Utilities/IntrospectionExtensions.cs new file mode 100644 index 0000000..e0ba8a7 --- /dev/null +++ b/src/Utilities/IntrospectionExtensions.cs @@ -0,0 +1,224 @@ +using System.Collections.Generic; +using System.Threading; + +namespace System.Reflection +{ +#if NET40 + internal class TypeInfo + { + private const BindingFlags DeclaredOnlyLookup = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly; + private 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; + + #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 GetDeclaredMethods(String name) + { + foreach (MethodInfo method in _type.GetMethods(DeclaredOnlyLookup)) + { + if (method.Name == name) + yield return method; + } + } + + public virtual System.Reflection.TypeInfo GetDeclaredNestedType(String name) + { + var nt = _type.GetNestedType(name, DeclaredOnlyLookup); + if (nt == null) + { + return null; //the extension method GetTypeInfo throws for null + } + else + { + return nt.GetTypeInfo(); + } + } + + public virtual PropertyInfo GetDeclaredProperty(String name) + { + return _type.GetProperty(name, DeclaredOnlyLookup); + } + + + //// Properties + + public virtual IEnumerable DeclaredConstructors + { + get + { + return _type.GetConstructors(DeclaredOnlyLookup); + } + } + + public virtual IEnumerable DeclaredEvents + { + get + { + return _type.GetEvents(DeclaredOnlyLookup); + } + } + + public virtual IEnumerable DeclaredFields + { + get + { + return _type.GetFields(DeclaredOnlyLookup); + } + } + + public virtual IEnumerable DeclaredMembers + { + get + { + return _type.GetMembers(DeclaredOnlyLookup); + } + } + + public virtual IEnumerable DeclaredMethods + { + get + { + return _type.GetMethods(DeclaredOnlyLookup); + } + } + public virtual IEnumerable DeclaredNestedTypes + { + get + { + foreach (var t in _type.GetNestedTypes(DeclaredOnlyLookup)) + { + yield return t.GetTypeInfo(); + } + } + } + + public virtual IEnumerable DeclaredProperties + { + get + { + return _type.GetProperties(DeclaredOnlyLookup); + } + } + + + public virtual IEnumerable ImplementedInterfaces + { + get + { + return _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(); + } + + } +#endif + + + internal static class IntrospectionExtensions + { +#if NET40 + 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 + } +} \ No newline at end of file diff --git a/src/Utilities/StaticReflection.cs b/src/Utilities/StaticReflection.cs index 8512647..1610575 100644 --- a/src/Utilities/StaticReflection.cs +++ b/src/Utilities/StaticReflection.cs @@ -66,7 +66,7 @@ namespace Microsoft.Practices.Unity.Utility { var property = GetPropertyInfo(expression); - var getMethod = property.GetMethod; + var getMethod = property.GetGetMethod(true); if (getMethod == null) { throw new InvalidOperationException("Invalid expression form passed"); @@ -91,7 +91,7 @@ namespace Microsoft.Practices.Unity.Utility { var property = GetPropertyInfo(expression); - var setMethod = property.SetMethod; + var setMethod = property.GetSetMethod(true); if (setMethod == null) { throw new InvalidOperationException("Invalid expression form passed"); diff --git a/src/package.snk b/src/package.snk new file mode 100644 index 0000000..986f91e Binary files /dev/null and b/src/package.snk differ