diff --git a/src/Core/ReflectionExtensions.cs b/src/Core/ReflectionExtensions.cs index 8f0ac42..ab81987 100644 --- a/src/Core/ReflectionExtensions.cs +++ b/src/Core/ReflectionExtensions.cs @@ -41,6 +41,7 @@ internal static MethodInfo FindMethodRecursively(this Type prop, string name, pa { if (prop == null) return null; + // GetMethod searches base types... var method = prop.GetMethod(name, typeArgs); if (method != null) return method; @@ -50,16 +51,9 @@ internal static MethodInfo FindMethodRecursively(this Type prop, string name, pa method = iface.FindMethodRecursively(name, typeArgs); if (method != null) return method; } - // base types - var baseType = prop.GetTypeInfo().BaseType; - if (baseType != null) - { - method = baseType.FindMethodRecursively(name, typeArgs); - if (method != null) return method; - } // TODO better bailout exception? - throw new ArgumentException($"Unable to locate method with name: {name}"); + throw new ArgumentException($"Unable to locate method with name: {name}", nameof(name)); } /// diff --git a/test/unit/Core/ReflectionExtensionTest.cs b/test/unit/Core/ReflectionExtensionTest.cs index 4bc327e..9c18a9f 100644 --- a/test/unit/Core/ReflectionExtensionTest.cs +++ b/test/unit/Core/ReflectionExtensionTest.cs @@ -1,4 +1,5 @@ -using System.Linq; +using System; +using System.Linq; using System.Reflection; using System.Runtime.Serialization; using RapidCore.Reflection; @@ -23,11 +24,17 @@ public void ReflectionExtension_resolves_prop_names_correctly() Assert.Equal("MemberNoCustomName", propNoCustomName.GetPropSerializedName()); var propWithCustomName = dtoProps.Single(p => p.Name == "MemberWithCustomName"); Assert.Equal("CallMeThis", propWithCustomName.GetPropSerializedName()); + } + [Fact] + public void ReflectionExtension_resolves_prop_names_correctly_handles_null_prop() + { + PropertyInfo prop = null; + Assert.Null(prop.GetPropSerializedName()); } [Fact] - public void ReflectionExtension_can_find_methods_from_base_types_and_interfaces() + public void ReflectionExtension_can_find_methods_from_interfaces() { var dto = new ContactListHubSpotEntity(); @@ -38,6 +45,37 @@ public void ReflectionExtension_can_find_methods_from_base_types_and_interfaces( Assert.NotNull(method); } + [Fact] + public void ReflectionExtension_can_find_methods_from_base_classes() + { + // all classes inherits from Object and Object has ToString() method! + var instance = new Inheritor(); + var method = instance.GetType().FindMethodRecursively("ToString"); + + Assert.NotNull(method); + } + + [Fact] + public void ReflectionExtension_throws_when_unable_to_find_method() + { + // all classes inherits from Object and Object has ToString() method! + var instance = new Inheritor(); + var exception = Record.Exception(() => instance.GetType().FindMethodRecursively("IfThisIsHereStuffIsWhack")); + + Assert.NotNull(exception); + var argumentException = Assert.IsType(exception); + + Assert.Equal("name", argumentException.ParamName); + } + + + [Fact] + public void ReflectionExtension_can_find_methods_from_base_types_and_interfaces_handles_null() + { + Type type = null; + Assert.Null(type.FindMethodRecursively("Add")); + } + [Fact] public void ReflectionExtension_can_determine_if_prop_has_ignore_data_member() { @@ -51,6 +89,13 @@ public void ReflectionExtension_can_determine_if_prop_has_ignore_data_member() } [Fact] + public void ReflectionExtension_can_determine_if_prop_has_ignore_data_member_handles_null() + { + PropertyInfo prop = null; + Assert.False(prop.HasIgnoreDataMemberAttribute()); + } + + [Fact] public void ReflectionExtension_can_determine_if_stuff_is_complex() { string nuller = null; @@ -78,5 +123,11 @@ private class ClassWithDataMembers [IgnoreDataMember] public string IgnoreMePlease { get; set; } } + + + private class Inheritor + { + + } } }