Table of Contents

Class ReflectionEx

Namespace
Utils.Reflection
Assembly
Utils.dll

Provides extension methods to simplify reflection-based operations.

public static class ReflectionEx
Inheritance
ReflectionEx
Inherited Members

Methods

GetDirectInterfaces(Type)

Retrieves the interfaces that are directly implemented by the type, excluding those inherited from base types or from other directly implemented interfaces.

public static IEnumerable<Type> GetDirectInterfaces(this Type type)

Parameters

type Type

The type to check for directly implemented interfaces.

Returns

IEnumerable<Type>

An enumerable of directly implemented interfaces, in the order returned by reflection.

Remarks

For class types the method subtracts the interfaces already exposed by the base class. For interface types it subtracts the interfaces that are transitively inherited through any parent interface, retaining only the interfaces declared directly on type itself.

GetTypeHierarchy(Type)

Retrieves the type hierarchy starting from the given type, along with the directly implemented interfaces at each level.

public static IEnumerable<(Type Type, Type[] Interfaces)> GetTypeHierarchy(this Type type)

Parameters

type Type

The starting type for the hierarchy traversal.

Returns

IEnumerable<(Type Type, Type[] Interfaces)>

An enumerable of tuples containing the and its directly implemented interfaces.

GetTypeOf(MemberInfo)

Retrieves the type associated with the specified MemberInfo. This could be a property type, field type, return type for methods, and more.

public static Type GetTypeOf(this MemberInfo member)

Parameters

member MemberInfo

The MemberInfo from which to retrieve the type.

Returns

Type

The type associated with the member.

Exceptions

NotSupportedException

Thrown if the member type is unsupported.

GetTypes(IEnumerable<Assembly>, Func<Type, bool>, ICollection<Exception>?)

Gets types from each assembly in assemblies matching filter.

public static IEnumerable<Type> GetTypes(this IEnumerable<Assembly> assemblies, Func<Type, bool> filter, ICollection<Exception>? loadErrors = null)

Parameters

assemblies IEnumerable<Assembly>

Assemblies to enumerate types from.

filter Func<Type, bool>

Predicate applied to each type.

loadErrors ICollection<Exception>

When not null, per-assembly type-load failures are collected here and the successfully loadable types from each assembly are still returned (tolerant mode). When null (default), any ReflectionTypeLoadException propagates immediately (strict mode).

Returns

IEnumerable<Type>

Types from all assemblies that satisfy filter.

GetTypes(Func<Type, bool>)

Get types given the specified predicate

public static IEnumerable<Type> GetTypes(Func<Type, bool> filter)

Parameters

filter Func<Type, bool>

Filter to apply to types

Returns

IEnumerable<Type>

IEnumerable<T> of Type that match the given filter

GetTypes(Assembly, Func<Type, bool>, ICollection<Exception>?)

Gets types from assembly matching filter.

public static IEnumerable<Type> GetTypes(this Assembly assembly, Func<Type, bool> filter, ICollection<Exception>? loadErrors = null)

Parameters

assembly Assembly

Assembly to enumerate types from.

filter Func<Type, bool>

Predicate applied to each type.

loadErrors ICollection<Exception>

When not null, type-load failures are collected here instead of thrown, and any successfully loadable types are still returned (tolerant mode). When null (default), a ReflectionTypeLoadException from the underlying GetTypes() call propagates to the caller (strict mode).

Returns

IEnumerable<Type>

Types from assembly that satisfy filter.

LoadAssemblies(string, bool)

Loads managed assemblies from the directory identified by path.

public static IEnumerable<Assembly> LoadAssemblies(string path, bool raiseError = false)

Parameters

path string

Directory path (may include wildcards) to enumerate.

raiseError bool

true to rethrow any exception thrown while loading an individual file; false (default) to skip files that cannot be loaded.

Returns

IEnumerable<Assembly>

A sequence of successfully loaded assemblies.

Remarks

Only files with a .dll or .exe extension are probed; other files are silently skipped. Each candidate is loaded with LoadFrom(string), which resolves the canonical on-disk path and avoids the identity mismatch that Assembly.Load(string) causes when called with a file path instead of an assembly display name.