Shuttle.Core.Reflection
Provides various methods to facilitate reflection handling.
Installation
bash
dotnet add package Shuttle.Core.ReflectionAssembly Extensions
c#
Task<IEnumerable<Type>> GetTypesCastableToAsync(this Assembly assembly, Type type)
Task<IEnumerable<Type>> GetTypesCastableToAsync<T>(this Assembly assembly)GetTypesCastableToAsync: Returns all the types in the givenassemblythat can be cast to thetypeortypeof(T).
Assembly Static Extensions
c#
Task<IEnumerable<Assembly>> GetRuntimeAssembliesAsync()
Task<IEnumerable<Type>> GetTypesCastableToAsync(Type type)
Task<IEnumerable<Type>> GetTypesCastableToAsync<T>()GetRuntimeAssembliesAsync: Returns a combination ofDependencyContext.Default.GetRuntimeAssemblyNames(Environment.OSVersion.Platform.ToString())andAppDomain.CurrentDomain.GetAssemblies().GetTypesCastableToAsync: Returns all the types in all assemblies returned byGetRuntimeAssembliesAsync()that can be cast to thetypeortypeof(T).
Enumerable Extensions
c#
T? Find<T>(this IEnumerable<object> list) where T : class
IEnumerable<T> FindAll<T>(this IEnumerable<object> list) where T : class
T Get<T>(this IEnumerable<object> list) where T : classFind<T>: Returns the single instance of typeTfrom thelist. Throws an exception if more than one instance is found. Returnsnullif no instance is found.FindAll<T>: Returns all instances of typeTfrom thelist.Get<T>: Returns the single instance of typeTfrom thelist. Throws an exception if more than one instance is found or if no instance is found.
Exception Extensions
c#
string AllMessages(this Exception ex)
bool Contains<T>(this Exception ex) where T : Exception
T? Find<T>(this Exception ex) where T : Exception
Exception TrimLeading<T>(this Exception ex) where T : ExceptionAllMessages: Traverses the exception and its inner exceptions to concatenate all messages.Contains<T>: Determines whether the exception or any of its inner exceptions are of typeT.Find<T>: Returns the first exception of typeTfound in the exception chain.TrimLeading<T>: Removes the outer exception(s) if they are of typeTand returns the inner exception.
Object Extensions
c#
void TryDispose(this object o)
Task TryDisposeAsync(this object o)TryDispose: Attempts to cast the object toIDisposableand callsDisposeif successful.TryDisposeAsync: Attempts to cast the object toIAsyncDisposableand callsDisposeAsync. If not implemented, falls back toTryDispose.
Type Extensions
c#
void AssertDefaultConstructor(this Type type)
void AssertDefaultConstructor(this Type type, string message)
Type? FirstInterface(this Type type, Type of)
Type? GetGenericArgument(this Type type, Type generic)
bool HasDefaultConstructor(this Type type)
Type? InterfaceMatching(this Type type, string includeRegexPattern, string? excludeRegexPattern = null)
IEnumerable<Type> InterfacesCastableTo<T>(this Type type)
IEnumerable<Type> InterfacesCastableTo(this Type type, Type interfaceType)
bool IsCastableTo(this Type type, Type otherType)
Type? MatchingInterface(this Type type)AssertDefaultConstructor: Throws an exception if the type does not have a default constructor.FirstInterface: Returns the first interface that matches the naming conventionI{TypeName}or is castable to the specified type.GetGenericArgument: Returns the generic argument for the specified generic type definition.HasDefaultConstructor: Determines whether the type has a default constructor.InterfaceMatching: Returns the first interface matching the include regex and not matching the exclude regex.InterfacesCastableTo: Returns all interfaces implemented by the type that are castable to the specified type.IsCastableTo: Determines whether the type is castable to theotherType.MatchingInterface: Returns the interface that matches the naming conventionI{TypeName}.