From bb97774c1b2b74491a5fa96968bff572c7bce5d7 Mon Sep 17 00:00:00 2001 From: Eugene Sadovoi Date: Sat, 2 Mar 2019 13:48:28 -0500 Subject: [PATCH] Releasing 4.1.0 --- package.props | 2 +- .../Injection/Members/InjectionConstructor.cs | 9 ++--- .../Injection/Members/InjectionField.cs | 19 ++-------- .../Injection/Members/InjectionMethod.cs | 19 ++-------- .../Injection/Members/InjectionProperty.cs | 28 +++++--------- .../Reflection/TypeReflectionExtensions.cs | 37 +++++++++++++++++++ 6 files changed, 58 insertions(+), 56 deletions(-) diff --git a/package.props b/package.props index ddb5a0fe..7437c8de 100644 --- a/package.props +++ b/package.props @@ -1,7 +1,7 @@ - 4.0.3 + 4.1.0 netstandard2.0;netstandard1.0;netcoreapp2.0;netcoreapp1.0;net47;net46;net45;net40 diff --git a/src/Dependency/Injection/Members/InjectionConstructor.cs b/src/Dependency/Injection/Members/InjectionConstructor.cs index 28192189..b7b596b3 100644 --- a/src/Dependency/Injection/Members/InjectionConstructor.cs +++ b/src/Dependency/Injection/Members/InjectionConstructor.cs @@ -50,12 +50,9 @@ protected override ConstructorInfo SelectMember(Type type, InjectionMember _) public override IEnumerable DeclaredMembers(Type type) { -#if NETCOREAPP1_0 || NETSTANDARD1_0 - return type.GetTypeInfo().DeclaredConstructors - .Where(c => c.IsStatic == false && c.IsPublic); -#else - return type.GetConstructors(BindingFlags.Public | BindingFlags.Instance); -#endif + return type.GetTypeInfo() + .DeclaredConstructors + .Where(ctor => !ctor.IsFamily && !ctor.IsPrivate && !ctor.IsStatic); } public override string ToString() diff --git a/src/Dependency/Injection/Members/InjectionField.cs b/src/Dependency/Injection/Members/InjectionField.cs index fe3b1861..dc084fc5 100644 --- a/src/Dependency/Injection/Members/InjectionField.cs +++ b/src/Dependency/Injection/Members/InjectionField.cs @@ -47,23 +47,12 @@ protected override FieldInfo DeclaredMember(Type type, string name) public override IEnumerable DeclaredMembers(Type type) { -#if NETCOREAPP1_0 || NETSTANDARD1_0 - if (type == null) + foreach (var member in type.GetDeclaredFields()) { - return Enumerable.Empty(); + if (!member.IsFamily && !member.IsPrivate && + !member.IsInitOnly && !member.IsStatic) + yield return member; } - - var info = type.GetTypeInfo(); - if (type == typeof(object)) - { - return info.DeclaredFields; - } - - return info.DeclaredFields - .Concat(DeclaredMembers(type.GetTypeInfo().BaseType)); -#else - return type.GetFields(BindingFlags.Instance | BindingFlags.Public); -#endif } protected override Type MemberType => Selection.FieldType; diff --git a/src/Dependency/Injection/Members/InjectionMethod.cs b/src/Dependency/Injection/Members/InjectionMethod.cs index 3a2431b7..c255adb7 100644 --- a/src/Dependency/Injection/Members/InjectionMethod.cs +++ b/src/Dependency/Injection/Members/InjectionMethod.cs @@ -51,23 +51,12 @@ protected override MethodInfo SelectMember(Type type, InjectionMember _) public override IEnumerable DeclaredMembers(Type type) { -#if NETCOREAPP1_0 || NETSTANDARD1_0 - if (null == type) return Enumerable.Empty(); - - var info = type.GetTypeInfo(); - if (typeof(object) == info.DeclaringType) + foreach (var member in type.GetDeclaredMethods()) { - return info.DeclaredMethods.Where(m => !m.IsStatic) - .Where(m => Name == m.Name); + if (!member.IsFamily && !member.IsPrivate && + !member.IsStatic && member.Name == Name) + yield return member; } - - return info.DeclaredMethods.Where(m => !m.IsStatic) - .Concat(DeclaredMembers(info.BaseType)) - .Where(m => Name == m.Name); -#else - return type.GetMethods(BindingFlags.Public | BindingFlags.Instance) - .Where(m => Name == m.Name); -#endif } public override string ToString() diff --git a/src/Dependency/Injection/Members/InjectionProperty.cs b/src/Dependency/Injection/Members/InjectionProperty.cs index 1127bba3..1dd5cbe5 100644 --- a/src/Dependency/Injection/Members/InjectionProperty.cs +++ b/src/Dependency/Injection/Members/InjectionProperty.cs @@ -43,32 +43,22 @@ public InjectionProperty(string name, object value) protected override PropertyInfo DeclaredMember(Type type, string name) { -#if NETSTANDARD1_0 || NETCOREAPP1_0 - return type.GetTypeInfo().GetDeclaredProperty(Selection.Name); -#else - return type.GetProperty(Selection.Name); -#endif + return DeclaredMembers(type).FirstOrDefault(p => p.Name == Selection.Name); } public override IEnumerable DeclaredMembers(Type type) { -#if NETCOREAPP1_0 || NETSTANDARD1_0 - if (type == null) + foreach (var member in type.GetDeclaredProperties()) { - return Enumerable.Empty(); - } + if (!member.CanWrite || 0 != member.GetIndexParameters().Length) + continue; - var info = type.GetTypeInfo(); - if (type == typeof(object)) - { - return info.DeclaredProperties; - } + var setter = member.GetSetMethod(true); + if (setter.IsPrivate || setter.IsFamily) + continue; - return info.DeclaredProperties - .Concat(DeclaredMembers(type.GetTypeInfo().BaseType)); -#else - return type.GetProperties(BindingFlags.Instance | BindingFlags.Public); -#endif + yield return member; + } } protected override Type MemberType => Selection.PropertyType; diff --git a/src/Extensions/Reflection/TypeReflectionExtensions.cs b/src/Extensions/Reflection/TypeReflectionExtensions.cs index e5d5f8e9..43b27ca9 100644 --- a/src/Extensions/Reflection/TypeReflectionExtensions.cs +++ b/src/Extensions/Reflection/TypeReflectionExtensions.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Reflection; namespace Unity @@ -18,5 +19,41 @@ public static Type GetArrayParameterType(this Type typeToReflect, Type[] generic return 1 == rank ? type.MakeArrayType() : type.MakeArrayType(rank); } + + public static IEnumerable GetDeclaredFields(this Type type) + { + var info = type.GetTypeInfo(); + while (null != info) + { + foreach (var member in info.DeclaredFields) + yield return member; + + info = info.BaseType?.GetTypeInfo(); + } + } + + public static IEnumerable GetDeclaredProperties(this Type type) + { + var info = type.GetTypeInfo(); + while (null != info) + { + foreach (var member in info.DeclaredProperties) + yield return member; + + info = info.BaseType?.GetTypeInfo(); + } + } + + public static IEnumerable GetDeclaredMethods(this Type type) + { + var info = type.GetTypeInfo(); + while (null != info) + { + foreach (var member in info.DeclaredMethods) + yield return member; + + info = info.BaseType?.GetTypeInfo(); + } + } } }