Skip to content

Commit 059687e

Browse files
authored
Merge pull request #6 from Shuttle/v21
V21
2 parents 0e35743 + c9c9e54 commit 059687e

37 files changed

Lines changed: 543 additions & 769 deletions

README.md

Lines changed: 72 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,91 @@
11
# Shuttle.Core.Reflection
22

3+
Provides various methods to facilitate reflection handling.
4+
5+
## Installation
6+
7+
```bash
8+
dotnet add package Shuttle.Core.Reflection
39
```
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)
516
```
617

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
821

9-
## ReflectionService
22+
These are static extensions on the `Assembly` class.
1023

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>()
1328
```
1429

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)`.
1632

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
1939
```
2040

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
2246

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
2552
```
2653

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
2860

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)
3564
```
3665

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+
```
3883

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}`.

Shuttle.Core.Reflection.Tests/ReflectionServiceFixture.cs

Lines changed: 0 additions & 34 deletions
This file was deleted.

Shuttle.Core.Reflection.Tests/Shuttle.Core.Reflection.Tests.csproj

Lines changed: 0 additions & 17 deletions
This file was deleted.

Shuttle.Core.Reflection.sln

Lines changed: 0 additions & 37 deletions
This file was deleted.

Shuttle.Core.Reflection/.package/AssemblyInfo.cs.template

Lines changed: 0 additions & 11 deletions
This file was deleted.
Binary file not shown.

Shuttle.Core.Reflection/.package/Shuttle.NuGetPackager.targets

Lines changed: 0 additions & 13 deletions
This file was deleted.

Shuttle.Core.Reflection/.package/package.msbuild

Lines changed: 0 additions & 104 deletions
This file was deleted.

Shuttle.Core.Reflection/.package/package.nuspec

Lines changed: 0 additions & 28 deletions
This file was deleted.

Shuttle.Core.Reflection/.package/package.nuspec.template

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)