|
1 | 1 | # Shuttle.Core.Reflection |
2 | 2 |
|
| 3 | +Provides various methods to facilitate reflection handling. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +```bash |
| 8 | +dotnet add package Shuttle.Core.Reflection |
3 | 9 | ``` |
4 | | -PM> Install-Package Shuttle.Core.Reflection |
| 10 | + |
| 11 | +## Assembly Extensions |
| 12 | + |
| 13 | +```csharp |
| 14 | +Task<IEnumerable<Type>> GetTypesCastableToAsync(this Assembly assembly, Type type) |
| 15 | +Task<IEnumerable<Type>> GetTypesCastableToAsync<T>(this Assembly assembly) |
5 | 16 | ``` |
6 | 17 |
|
7 | | -Provides various methods to facilitate reflection handling. |
| 18 | +- `GetTypesCastableToAsync`: Returns all the types in the given `assembly` that can be cast to the `type` or `typeof(T)`. |
| 19 | + |
| 20 | +## Assembly Static Extensions |
8 | 21 |
|
9 | | -## ReflectionService |
| 22 | +These are static extensions on the `Assembly` class. |
10 | 23 |
|
11 | | -``` c# |
12 | | -Task<IEnumerable<Assembly>> GetMatchingAssembliesAsync(Regex regex) |
| 24 | +```csharp |
| 25 | +Task<IEnumerable<Assembly>> Assembly.GetRuntimeAssembliesAsync() |
| 26 | +Task<IEnumerable<Type>> Assembly.GetTypesCastableToAsync(Type type) |
| 27 | +Task<IEnumerable<Type>> Assembly.GetTypesCastableToAsync<T>() |
13 | 28 | ``` |
14 | 29 |
|
15 | | -Returns a collection of assemblies that have their file name matching the given `Regex` expression. |
| 30 | +- `Assembly.GetRuntimeAssembliesAsync`: Returns a combination of `DependencyContext.Default.GetRuntimeAssemblyNames(Environment.OSVersion.Platform.ToString())` and `AppDomain.CurrentDomain.GetAssemblies()`. |
| 31 | +- `Assembly.GetTypesCastableToAsync`: Returns all the types in all assemblies returned by `Assembly.GetRuntimeAssembliesAsync()` that can be cast to the `type` or `typeof(T)`. |
16 | 32 |
|
17 | | -``` c# |
18 | | -Task<IEnumerable<Assembly>> GetRuntimeAssembliesAsync() |
| 33 | +## Enumerable Extensions |
| 34 | + |
| 35 | +```csharp |
| 36 | +T? Find<T>(this IEnumerable<object> list) where T : class |
| 37 | +IEnumerable<T> FindAll<T>(this IEnumerable<object> list) where T : class |
| 38 | +T Get<T>(this IEnumerable<object> list) where T : class |
19 | 39 | ``` |
20 | 40 |
|
21 | | -Returns a combination of `DependencyContext.Default.GetRuntimeAssemblyNames(Environment.OSVersion.Platform.ToString())` and `AppDomain.CurrentDomain.GetAssemblies()`. |
| 41 | +- `Find<T>`: Returns the single instance of type `T` from the `list`. Throws an exception if more than one instance is found. Returns `null` if no instance is found. |
| 42 | +- `FindAll<T>`: Returns all instances of type `T` from the `list`. |
| 43 | +- `Get<T>`: Returns the single instance of type `T` from the `list`. Throws an exception if more than one instance is found or if no instance is found. |
| 44 | + |
| 45 | +## Exception Extensions |
22 | 46 |
|
23 | | -``` c# |
24 | | -Task<Type> GetTypeAsync(string typeName) |
| 47 | +```csharp |
| 48 | +string AllMessages(this Exception ex) |
| 49 | +bool Contains<T>(this Exception ex) where T : Exception |
| 50 | +T? Find<T>(this Exception ex) where T : Exception |
| 51 | +Exception TrimLeading<T>(this Exception ex) where T : Exception |
25 | 52 | ``` |
26 | 53 |
|
27 | | -Attempts to find the requested type. |
| 54 | +- `AllMessages`: Traverses the exception and its inner exceptions to concatenate all messages, separated by ` / `. |
| 55 | +- `Contains<T>`: Determines whether the exception or any of its inner exceptions are of type `T`. |
| 56 | +- `Find<T>`: Returns the first exception of type `T` found in the exception chain. |
| 57 | +- `TrimLeading<T>`: Removes the outer exception(s) if they are of type `T` and returns the inner exception. |
| 58 | + |
| 59 | +## Object Extensions |
28 | 60 |
|
29 | | -``` c# |
30 | | -Task<IEnumerable<Type>> GetTypesCastableToAsync(Type type, Assembly assembly) |
31 | | -// and these extensions |
32 | | -Task<IEnumerable<Type>> GetTypesCastableToAsync<T>(); |
33 | | -Task<IEnumerable<Type>> GetTypesCastableToAsync(Type type); |
34 | | -Task<IEnumerable<Type>> GetTypesCastableToAsync<T>(Assembly assembly); |
| 61 | +```csharp |
| 62 | +void TryDispose(this object o) |
| 63 | +Task TryDisposeAsync(this object o) |
35 | 64 | ``` |
36 | 65 |
|
37 | | -Returns all the types in the given `assembly` that can be cast to the `type` or `typeof(T)`; if no `assembly` is provided the all assemblies returned by `GetAssembliesAsync()` will be scanned. |
| 66 | +- `TryDispose`: Attempts to cast the object to `IDisposable` and calls `Dispose` if successful. |
| 67 | +- `TryDisposeAsync`: Attempts to cast the object to `IAsyncDisposable` and calls `DisposeAsync`. If `IAsyncDisposable` is not implemented, it falls back to `TryDispose`. |
| 68 | + |
| 69 | +## Type Extensions |
| 70 | + |
| 71 | +```csharp |
| 72 | +void AssertDefaultConstructor(this Type type) |
| 73 | +void AssertDefaultConstructor(this Type type, string message) |
| 74 | +Type? FirstInterface(this Type type, Type of) |
| 75 | +Type? GetGenericArgument(this Type type, Type generic) |
| 76 | +bool HasDefaultConstructor(this Type type) |
| 77 | +Type? InterfaceMatching(this Type type, string includeRegexPattern, string? excludeRegexPattern = null) |
| 78 | +IEnumerable<Type> InterfacesCastableTo<T>(this Type type) |
| 79 | +IEnumerable<Type> InterfacesCastableTo(this Type type, Type interfaceType) |
| 80 | +bool IsCastableTo(this Type type, Type otherType) |
| 81 | +Type? MatchingInterface(this Type type) |
| 82 | +``` |
38 | 83 |
|
| 84 | +- `AssertDefaultConstructor`: Throws an exception if the type does not have a default constructor. |
| 85 | +- `FirstInterface`: Returns the first interface matching the naming convention `I{TypeName}` or, if no such interface is found, the first interface that is castable to the specified type. |
| 86 | +- `GetGenericArgument`: Returns the generic argument for the specified generic type definition. |
| 87 | +- `HasDefaultConstructor`: Determines whether the type has a default constructor. |
| 88 | +- `InterfaceMatching`: Returns the first interface matching the include regex and not matching the exclude regex. |
| 89 | +- `InterfacesCastableTo`: Returns all interfaces implemented by the type that are castable to the specified type. |
| 90 | +- `IsCastableTo`: Determines whether the type is castable to the `otherType`. |
| 91 | +- `MatchingInterface`: Returns the interface that matches the naming convention `I{TypeName}`. |
0 commit comments