Skip to content

Commit 201b952

Browse files
authored
Merge pull request #4 from Shuttle/async
Async
2 parents 1a8ab1e + 571f35a commit 201b952

File tree

10 files changed

+321
-53
lines changed

10 files changed

+321
-53
lines changed

README.md

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,52 +15,47 @@ string AssemblyPath(Assembly assembly)
1515
Returns the path to the given assembly.
1616

1717
``` c#
18-
Assembly FindAssemblyNamed(string name)
18+
Task<Assembly> FindAssemblyNamedAsync(string name)
1919
```
2020

2121
Returns the `Assembly` that has the requested `name`; else `null`.
2222

2323
``` c#
24-
Assembly GetAssembly(string assemblyPath)
24+
Task<Assembly> GetAssemblyAsync(string assemblyPath)
2525
```
2626

2727
Returns the requested assembly if found; else `null`.
2828

29-
```c#
30-
IEnumerable<Assembly> GetAssemblies()
31-
```
32-
33-
Returns all reachable assemblies.
34-
3529
``` c#
36-
IEnumerable<Assembly> GetMatchingAssemblies(string regex)
30+
Task<IEnumerable<Assembly>> GetMatchingAssembliesAsync(Regex regex)
3731
```
3832

3933
Returns a collection of assemblies that have their file name matching the given `Regex` expression.
4034

4135
``` c#
42-
IEnumerable<Assembly> GetRuntimeAssemblies()
36+
Task<IEnumerable<Assembly>> GetRuntimeAssembliesAsync()
4337
```
4438

45-
For .Net 4.6+ returns `AppDomain.CurrentDomain.GetAssemblies();`. For .Net Core 2.0+ all the `DependencyContext.Default.GetRuntimeAssemblyNames(RuntimeEnvironment.GetRuntimeIdentifier())` assembly names are resolved.
39+
For .Net 4.6+ (which isn't support in the latest version) returns `AppDomain.CurrentDomain.GetAssemblies();`. For .Net Core 2.0+ all the `DependencyContext.Default.GetRuntimeAssemblyNames(RuntimeEnvironment.GetRuntimeIdentifier())` assembly names are resolved.
4640

4741
``` c#
48-
Type GetType(string typeName)
42+
Task<Type> GetTypeAsync(string typeName)
4943
```
5044

5145
Attempts to find the requested type.
5246

5347
``` c#
54-
IEnumerable<Type> GetTypes(Assembly assembly)
48+
Task<IEnumerable<Type>> GetTypesAsync(Assembly assembly)
5549
```
5650

5751
Returns all types in the given `assembly`.
5852

5953
``` c#
60-
IEnumerable<Type> GetTypesAssignableTo<T>();
61-
IEnumerable<Type> GetTypesAssignableTo(Type type);
62-
IEnumerable<Type> GetTypesAssignableTo<T>(Assembly assembly);
63-
IEnumerable<Type> GetTypesAssignableTo(Type type, Assembly assembly);
54+
Task<IEnumerable<Type>> GetTypesAssignableToAsync(Type type, Assembly assembly)
55+
// and these extensions
56+
Task<IEnumerable<Type>> GetTypesAssignableToAsync<T>();
57+
Task<IEnumerable<Type>> GetTypesAssignableToAsync(Type type);
58+
Task<IEnumerable<Type>> GetTypesAssignableToAsync<T>(Assembly assembly);
6459
```
6560

6661
Returns all the types in the given `assembly` that are assignable to the `type` or `typeof(T)`; if no `assembly` is provided the all assemblies returned by `GetAssemblies()` will be scanned.

Shuttle.Core.Reflection.Tests/ReflectionServiceFixture.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Linq;
33
using System.Reflection;
4+
using System.Threading.Tasks;
45
using NUnit.Framework;
56

67
namespace Shuttle.Core.Reflection.Tests
@@ -14,24 +15,49 @@ public void Should_be_able_to_get_runtime_assemblies()
1415
Assert.That(new ReflectionService().GetRuntimeAssemblies().Count(), Is.GreaterThan(0));
1516
}
1617

18+
[Test]
19+
public async Task Should_be_able_to_get_runtime_assemblies_async()
20+
{
21+
Assert.That((await new ReflectionService().GetRuntimeAssembliesAsync()).Count(), Is.GreaterThan(0));
22+
}
23+
1724
[Test]
1825
public void Should_be_able_to_get_types()
1926
{
2027
Assert.That(new ReflectionService().GetTypesAssignableTo<SomeClass>().Count(), Is.EqualTo(1));
2128
Assert.That(new ReflectionService().GetTypesAssignableTo<ISomeClass>().Count(), Is.EqualTo(1));
2229
}
2330

31+
[Test]
32+
public async Task Should_be_able_to_get_types_async()
33+
{
34+
Assert.That((await new ReflectionService().GetTypesAssignableToAsync<SomeClass>()).Count(), Is.EqualTo(1));
35+
Assert.That((await new ReflectionService().GetTypesAssignableToAsync<ISomeClass>()).Count(), Is.EqualTo(1));
36+
}
37+
2438
[Test]
2539
public void Should_be_able_to_get_matching_assemblies()
2640
{
2741
var service = new ReflectionService();
28-
42+
2943
Assert.That(service.GetMatchingAssemblies(".+").Count(), Is.GreaterThan(0));
3044

3145
foreach (var assembly in service.GetMatchingAssemblies("nunit"))
3246
{
3347
Assert.That(assembly.FullName.Contains("nunit"));
3448
}
3549
}
50+
51+
public async Task Should_be_able_to_get_matching_assemblies_async()
52+
{
53+
var service = new ReflectionService();
54+
55+
Assert.That((await service.GetMatchingAssembliesAsync(".+")).Count(), Is.GreaterThan(0));
56+
57+
foreach (var assembly in (await service.GetMatchingAssembliesAsync("nunit")))
58+
{
59+
Assert.That(assembly.FullName.Contains("nunit"));
60+
}
61+
}
3662
}
3763
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
</PropertyGroup>
66

77
<ItemGroup>
8-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0" />
9-
<PackageReference Include="NUnit" Version="3.13.3" />
10-
<PackageReference Include="NUnit3TestAdapter" Version="4.3.1" />
8+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
9+
<PackageReference Include="NUnit" Version="3.14.0" />
10+
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
1111
</ItemGroup>
1212

1313
<ItemGroup>

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<package>
44
<metadata>
55
<id>Shuttle.Core.Reflection</id>
6-
<version>12.0.1</version>
6+
<version>13.0.0</version>
77
<authors>Eben Roux</authors>
88
<owners>Eben Roux</owners>
99
<license type="expression">BSD-3-Clause</license>
@@ -13,11 +13,11 @@
1313
<repository type="git" url="https://github.com/shuttle/Shuttle.Core.Reflection.git" />
1414
<projectUrl>https://github.com/shuttle/Shuttle.Core.Reflection</projectUrl>
1515
<description>Reflection infrastructure components.</description>
16-
<copyright>Copyright (c) 2022, Eben Roux</copyright>
16+
<copyright>Copyright (c) 2024, Eben Roux</copyright>
1717
<tags>shuttle reflection</tags>
1818
<dependencies>
1919
<dependency id="Microsoft.Extensions.DependencyModel" version="7.0.0" />
20-
<dependency id="Shuttle.Core.Contract" version="11.0.0" />
20+
<dependency id="Shuttle.Core.Contract" version="11.1.0" />
2121
</dependencies>
2222
</metadata>
2323
<files>

Shuttle.Core.Reflection/IReflectionService.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Reflection;
44
using System.Text.RegularExpressions;
5+
using System.Threading.Tasks;
56

67
namespace Shuttle.Core.Reflection
78
{
@@ -10,6 +11,7 @@ public interface IReflectionService
1011
event EventHandler<ExceptionRaisedEventArgs> ExceptionRaised;
1112

1213
string AssemblyPath(Assembly assembly);
14+
1315
Assembly GetAssembly(string assemblyPath);
1416
Assembly FindAssemblyNamed(string name);
1517
IEnumerable<Assembly> GetMatchingAssemblies(Regex regex);
@@ -18,5 +20,14 @@ public interface IReflectionService
1820
IEnumerable<Type> GetTypesAssignableTo(Type type, Assembly assembly);
1921
Type GetType(string typeName);
2022
IEnumerable<Assembly> GetRuntimeAssemblies();
23+
24+
Task<Assembly> GetAssemblyAsync(string assemblyPath);
25+
Task<Assembly> FindAssemblyNamedAsync(string name);
26+
Task<IEnumerable<Assembly>> GetMatchingAssembliesAsync(Regex regex);
27+
Task<IEnumerable<Type>> GetTypesAssignableToAsync(Type type);
28+
Task<IEnumerable<Type>> GetTypesAsync(Assembly assembly);
29+
Task<IEnumerable<Type>> GetTypesAssignableToAsync(Type type, Assembly assembly);
30+
Task<Type> GetTypeAsync(string typeName);
31+
Task<IEnumerable<Assembly>> GetRuntimeAssembliesAsync();
2132
}
2233
}
Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
11
using System;
2+
using System.Threading.Tasks;
23

34
namespace Shuttle.Core.Reflection
45
{
56
public static class ObjectExtensions
67
{
7-
public static void AttemptDispose(this object o)
8+
public static void TryDispose(this object o)
89
{
910
if (o is IDisposable disposable)
1011
{
1112
disposable.Dispose();
1213
}
1314
}
15+
16+
public static async Task TryDisposeAsync(this object o)
17+
{
18+
if (o is IAsyncDisposable disposable)
19+
{
20+
await disposable.DisposeAsync().ConfigureAwait(false);
21+
}
22+
else
23+
{
24+
o.TryDispose();
25+
}
26+
}
1427
}
1528
}

Shuttle.Core.Reflection/Properties/AssemblyInfo.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
[assembly: AssemblyTitle(".NET Standard")]
1414
#endif
1515

16-
[assembly: AssemblyVersion("12.0.1.0")]
17-
[assembly: AssemblyCopyright("Copyright (c) 2022, Eben Roux")]
16+
[assembly: AssemblyVersion("13.0.0.0")]
17+
[assembly: AssemblyCopyright("Copyright (c) 2024, Eben Roux")]
1818
[assembly: AssemblyProduct("Shuttle.Core.Reflection")]
1919
[assembly: AssemblyCompany("Eben Roux")]
2020
[assembly: AssemblyConfiguration("Release")]
21-
[assembly: AssemblyInformationalVersion("12.0.1")]
21+
[assembly: AssemblyInformationalVersion("13.0.0")]
2222
[assembly: ComVisible(false)]

0 commit comments

Comments
 (0)