Skip to content
This repository was archived by the owner on Nov 10, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 99 additions & 38 deletions Source/aweXpect.Reflection/Helpers/TypeHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
Expand All @@ -17,68 +18,128 @@ internal static class TypeHelpers
/// </summary>
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 [];
}
}

/// <summary>
/// Searches for events in the <paramref name="type" /> that were directly declared there.
/// </summary>
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 [];
}
}

/// <summary>
/// Searches for fields in the <paramref name="type" /> that were directly declared there.
/// </summary>
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 [];
}
}

/// <summary>
/// Searches for methods in the <paramref name="type" /> that were directly declared there.
/// </summary>
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 [];
}
}

/// <summary>
/// Searches for properties in the <paramref name="type" /> that were directly declared there.
/// </summary>
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 [];
}
}

/// <summary>
/// Checks if the <paramref name="type" /> has the specified <paramref name="accessModifiers" />.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Reflection;
using aweXpect.Reflection.Helpers;

Expand All @@ -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<InvalidOperationException>();
}

[Fact]
public async Task HasAttribute_WithAttribute_ShouldReturnTrue()
{
Expand Down Expand Up @@ -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
{
Expand Down
Loading