diff --git a/Source/aweXpect.Reflection/Helpers/TypeHelpers.cs b/Source/aweXpect.Reflection/Helpers/TypeHelpers.cs index e5c87cd4..28142a8f 100644 --- a/Source/aweXpect.Reflection/Helpers/TypeHelpers.cs +++ b/Source/aweXpect.Reflection/Helpers/TypeHelpers.cs @@ -1,4 +1,5 @@ using System; +using System.IO; using System.Linq; using System.Reflection; using System.Runtime.CompilerServices; @@ -17,68 +18,128 @@ internal static class TypeHelpers /// public static ConstructorInfo[] GetDeclaredConstructors( this Type type) - => type - .GetConstructors(BindingFlags.DeclaredOnly | - BindingFlags.NonPublic | - BindingFlags.Public | - BindingFlags.Instance | - BindingFlags.Static); + { + try + { + return type + .GetConstructors(BindingFlags.DeclaredOnly | + BindingFlags.NonPublic | + BindingFlags.Public | + BindingFlags.Instance | + BindingFlags.Static); + } + catch (Exception exception) when (exception + is TypeLoadException + or FileNotFoundException + or FileLoadException) + { + return []; + } + } /// /// Searches for events in the that were directly declared there. /// public static EventInfo[] GetDeclaredEvents( this Type type) - => type - .GetEvents(BindingFlags.DeclaredOnly | - BindingFlags.NonPublic | - BindingFlags.Public | - BindingFlags.Instance); + { + try + { + return type + .GetEvents(BindingFlags.DeclaredOnly | + BindingFlags.NonPublic | + BindingFlags.Public | + BindingFlags.Instance); + } + catch (Exception exception) when (exception + is TypeLoadException + or FileNotFoundException + or FileLoadException) + { + return []; + } + } /// /// Searches for fields in the that were directly declared there. /// public static FieldInfo[] GetDeclaredFields( this Type type) - => type - .GetFields(BindingFlags.DeclaredOnly | - BindingFlags.NonPublic | - BindingFlags.Public | - BindingFlags.Instance | - BindingFlags.Static) - .Where(m => !m.IsSpecialName) - .Where(m => !m.Name.EndsWith("__BackingField")) - .ToArray(); + { + try + { + return type + .GetFields(BindingFlags.DeclaredOnly | + BindingFlags.NonPublic | + BindingFlags.Public | + BindingFlags.Instance | + BindingFlags.Static) + .Where(m => !m.IsSpecialName) + .Where(m => !m.Name.EndsWith("__BackingField")) + .ToArray(); + } + catch (Exception exception) when (exception + is TypeLoadException + or FileNotFoundException + or FileLoadException) + { + return []; + } + } /// /// Searches for methods in the that were directly declared there. /// public static MethodInfo[] GetDeclaredMethods( this Type type) - => type - .GetMethods(BindingFlags.DeclaredOnly | - BindingFlags.NonPublic | - BindingFlags.Public | - BindingFlags.Static | - BindingFlags.Instance | - BindingFlags.Static) - .Where(m => !m.IsSpecialName) - .ToArray(); + { + try + { + return type + .GetMethods(BindingFlags.DeclaredOnly | + BindingFlags.NonPublic | + BindingFlags.Public | + BindingFlags.Static | + BindingFlags.Instance | + BindingFlags.Static) + .Where(m => !m.IsSpecialName) + .ToArray(); + } + catch (Exception exception) when (exception + is TypeLoadException + or FileNotFoundException + or FileLoadException) + { + return []; + } + } /// /// Searches for properties in the that were directly declared there. /// public static PropertyInfo[] GetDeclaredProperties( this Type type) - => type - .GetProperties(BindingFlags.DeclaredOnly | - BindingFlags.NonPublic | - BindingFlags.Public | - BindingFlags.Static | - BindingFlags.Instance | - BindingFlags.Static) - .Where(m => !m.IsSpecialName) - .ToArray(); + { + try + { + return type + .GetProperties(BindingFlags.DeclaredOnly | + BindingFlags.NonPublic | + BindingFlags.Public | + BindingFlags.Static | + BindingFlags.Instance | + BindingFlags.Static) + .Where(m => !m.IsSpecialName) + .ToArray(); + } + catch (Exception exception) when (exception + is TypeLoadException + or FileNotFoundException + or FileLoadException) + { + return []; + } + } /// /// Checks if the has the specified . diff --git a/Tests/aweXpect.Reflection.Internal.Tests/Helpers/TypeHelpersTests.cs b/Tests/aweXpect.Reflection.Internal.Tests/Helpers/TypeHelpersTests.cs index 3090bce6..1b64d1e8 100644 --- a/Tests/aweXpect.Reflection.Internal.Tests/Helpers/TypeHelpersTests.cs +++ b/Tests/aweXpect.Reflection.Internal.Tests/Helpers/TypeHelpersTests.cs @@ -1,5 +1,5 @@ using System.Collections.Generic; -using System.Linq; +using System.IO; using System.Reflection; using aweXpect.Reflection.Helpers; @@ -16,13 +16,43 @@ public async Task GetDeclaredFields_ShouldFilterOutBackingFields() FieldInfo[] allFields = type.GetFields(BindingFlags.DeclaredOnly | BindingFlags.NonPublic | BindingFlags.Public | - BindingFlags.Instance).ToArray(); + BindingFlags.Instance); FieldInfo[] result = type.GetDeclaredFields(); await That(allFields).HasCount(2); await That(result).HasCount(1); } + [Theory] + [InlineData(typeof(TypeLoadException))] + [InlineData(typeof(FileNotFoundException))] + [InlineData(typeof(FileLoadException))] + public async Task GetDeclaredMembers_WhenReflectionThrowsLoadException_ShouldReturnEmpty(Type exceptionType) + { + Exception exception = (Exception)Activator.CreateInstance(exceptionType)!; + Type type = new ThrowingType(typeof(TestClassWithAttribute), exception); + + await That(type.GetDeclaredConstructors()).IsEmpty(); + await That(type.GetDeclaredEvents()).IsEmpty(); + await That(type.GetDeclaredFields()).IsEmpty(); + await That(type.GetDeclaredMethods()).IsEmpty(); + await That(type.GetDeclaredProperties()).IsEmpty(); + } + + [Fact] + public async Task GetDeclaredMembers_WhenReflectionThrowsUnrelatedException_ShouldNotCatch() + { + Type type = new ThrowingType(typeof(TestClassWithAttribute), new InvalidOperationException()); + + async Task Act() + { + await Task.Yield(); + type.GetDeclaredMethods(); + } + + await That(Act).Throws(); + } + [Fact] public async Task HasAttribute_WithAttribute_ShouldReturnTrue() { @@ -192,6 +222,15 @@ private interface IFooInterface { } + private sealed class ThrowingType(Type inner, Exception exception) : TypeDelegator(inner) + { + public override ConstructorInfo[] GetConstructors(BindingFlags bindingAttr) => throw exception; + public override EventInfo[] GetEvents(BindingFlags bindingAttr) => throw exception; + public override FieldInfo[] GetFields(BindingFlags bindingAttr) => throw exception; + public override MethodInfo[] GetMethods(BindingFlags bindingAttr) => throw exception; + public override PropertyInfo[] GetProperties(BindingFlags bindingAttr) => throw exception; + } + [Dummy(1)] private class TestClassWithAttribute {