Skip to content

Commit 0ffd8ac

Browse files
authored
Merge pull request #3 from Shuttle/ms-di
- Microsoft dependency injection / options
2 parents 504021d + 4569f81 commit 0ffd8ac

File tree

10 files changed

+51
-79
lines changed

10 files changed

+51
-79
lines changed

README.md

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,16 @@ Assembly FindAssemblyNamed(string name)
2121
Returns the `Assembly` that has the requested `name`; else `null`.
2222

2323
``` c#
24-
IEnumerable<Assembly> GetAssemblies()
25-
```
26-
27-
Returns all runtime assemblies as well as those in the `AppDomain.CurrentDomain.BaseDirectory` and `AppDomain.CurrentDomain.RelativeSearchPath`.
28-
29-
``` c#
30-
IEnumerable<Assembly> GetAssemblies(string folder)
24+
Assembly GetAssembly(string assemblyPath)
3125
```
3226

33-
Returns a collection of all assemblies located in the given folder.
27+
Returns the requested assembly if found; else `null`.
3428

35-
``` c#
36-
Assembly GetAssembly(string assemblyPath)
29+
```c#
30+
IEnumerable<Assembly> GetAssemblies()
3731
```
3832

39-
Returns the requested assembly if found; else `null`.
33+
Returns all reachable assemblies.
4034

4135
``` c#
4236
IEnumerable<Assembly> GetMatchingAssemblies(string regex)
@@ -45,16 +39,16 @@ IEnumerable<Assembly> GetMatchingAssemblies(string regex)
4539
Returns a collection of assemblies that have their file name matching the given `Regex` expression.
4640

4741
``` c#
48-
IEnumerable<Assembly> GetMatchingAssemblies(string regex, string folder)
42+
IEnumerable<Assembly> GetRuntimeAssemblies()
4943
```
5044

51-
Returns a collection of assemblies in the given `folder` that have their file name matching the given `regex` expression.
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.
5246

5347
``` c#
54-
IEnumerable<Assembly> GetRuntimeAssemblies()
48+
Type GetType(string typeName)
5549
```
5650

57-
For .Net 4.6+ returns `AppDomain.CurrentDomain.GetAssemblies();`. For .Net Core 2.0+ all the `DependencyContext.Default.GetRuntimeAssemblyNames(RuntimeEnvironment.GetRuntimeIdentifier())` assembly names are resolved.
51+
Attempts to find the requested type.
5852

5953
``` c#
6054
IEnumerable<Type> GetTypes(Assembly assembly)
@@ -63,10 +57,10 @@ IEnumerable<Type> GetTypes(Assembly assembly)
6357
Returns all types in the given `assembly`.
6458

6559
``` c#
66-
IEnumerable<Type> GetTypesAssignableTo<T>()
67-
IEnumerable<Type> GetTypesAssignableTo(Type type)
68-
IEnumerable<Type> GetTypesAssignableTo<T>(Assembly assembly)
69-
IEnumerable<Type> GetTypesAssignableTo(Type type, Assembly assembly)
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);
7064
```
7165

7266
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/.package/package.nuspec

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,26 @@
33
<package>
44
<metadata>
55
<id>Shuttle.Core.Reflection</id>
6-
<version>11.0.3</version>
6+
<version>12.0.0</version>
77
<authors>Eben Roux</authors>
88
<owners>Eben Roux</owners>
99
<license type="expression">BSD-3-Clause</license>
1010
<requireLicenseAcceptance>false</requireLicenseAcceptance>
1111
<icon>images\logo.png</icon>
12+
<readme>docs\README.md</readme>
1213
<repository type="git" url="https://github.com/shuttle/Shuttle.Core.Reflection.git" />
1314
<projectUrl>https://github.com/shuttle/Shuttle.Core.Reflection</projectUrl>
1415
<description>Reflection infrastructure components.</description>
1516
<copyright>Copyright (c) 2022, Eben Roux</copyright>
1617
<tags>shuttle reflection</tags>
1718
<dependencies>
1819
<dependency id="Microsoft.Extensions.DependencyModel" version="6.0.0" />
19-
<dependency id="Shuttle.Core.Logging" version="10.0.4" />
20+
<dependency id="Shuttle.Core.Contract" version="10.1.0" />
2021
</dependencies>
2122
</metadata>
2223
<files>
2324
<file src="..\..\..\.media\logo.png" target="images" />
25+
<file src="..\..\..\README.md" target="docs\" />
2426
<file src="lib\**\*.*" target="lib" />
2527
</files>
2628
</package>

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<license type="expression">BSD-3-Clause</license>
1010
<requireLicenseAcceptance>false</requireLicenseAcceptance>
1111
<icon>images\logo.png</icon>
12+
<readme>docs\README.md</readme>
1213
<repository type="git" url="https://github.com/shuttle/Shuttle.Core.Reflection.git" />
1314
<projectUrl>https://github.com/shuttle/Shuttle.Core.Reflection</projectUrl>
1415
<description>Reflection infrastructure components.</description>
@@ -20,6 +21,7 @@
2021
</metadata>
2122
<files>
2223
<file src="..\..\..\.media\logo.png" target="images" />
24+
<file src="..\..\..\README.md" target="docs\" />
2325
<file src="lib\**\*.*" target="lib" />
2426
</files>
2527
</package>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using Shuttle.Core.Contract;
3+
4+
namespace Shuttle.Core.Reflection
5+
{
6+
public class ExceptionRaisedEventArgs : EventArgs
7+
{
8+
public string MethodName { get; }
9+
public Exception Exception { get; }
10+
11+
public ExceptionRaisedEventArgs(string methodName, Exception exception)
12+
{
13+
Guard.AgainstNullOrEmptyString(methodName, nameof(methodName));
14+
Guard.AgainstNull(exception, nameof(exception));
15+
16+
MethodName = methodName;
17+
Exception = exception;
18+
}
19+
}
20+
}

Shuttle.Core.Reflection/IReflectionService.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ namespace Shuttle.Core.Reflection
77
{
88
public interface IReflectionService
99
{
10+
event EventHandler<ExceptionRaisedEventArgs> ExceptionRaised;
11+
1012
string AssemblyPath(Assembly assembly);
1113
Assembly GetAssembly(string assemblyPath);
1214
Assembly FindAssemblyNamed(string name);

Shuttle.Core.Reflection/Properties/AssemblyInfo.cs

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

16-
[assembly: AssemblyVersion("11.0.3.0")]
16+
[assembly: AssemblyVersion("12.0.0.0")]
1717
[assembly: AssemblyCopyright("Copyright (c) 2022, Eben Roux")]
1818
[assembly: AssemblyProduct("Shuttle.Core.Reflection")]
1919
[assembly: AssemblyCompany("Eben Roux")]
2020
[assembly: AssemblyConfiguration("Release")]
21-
[assembly: AssemblyInformationalVersion("11.0.3")]
21+
[assembly: AssemblyInformationalVersion("12.0.0")]
2222
[assembly: ComVisible(false)]

Shuttle.Core.Reflection/ReflectionService.cs

Lines changed: 7 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
using System.Text.RegularExpressions;
77
using Microsoft.Extensions.DependencyModel;
88
using Shuttle.Core.Contract;
9-
using Shuttle.Core.Logging;
10-
#if NETCOREAPP
11-
using System.Runtime.Loader;
12-
#endif
139

1410
namespace Shuttle.Core.Reflection
1511
{
@@ -21,12 +17,9 @@ public class ReflectionService : IReflectionService
2117
".exe"
2218
};
2319

24-
private readonly ILog _log;
25-
26-
public ReflectionService()
20+
public event EventHandler<ExceptionRaisedEventArgs> ExceptionRaised = delegate
2721
{
28-
_log = Log.For(this);
29-
}
22+
};
3023

3124
public string AssemblyPath(Assembly assembly)
3225
{
@@ -50,40 +43,11 @@ public Assembly GetAssembly(string assemblyPath)
5043

5144
try
5245
{
53-
var fileName = Path.GetFileNameWithoutExtension(assemblyPath);
54-
55-
#if NETCOREAPP
56-
var inCompileLibraries = DependencyContext.Default.CompileLibraries.Any(library => library.Name.Equals(fileName, StringComparison.OrdinalIgnoreCase));
57-
var inRuntimeLibraries = DependencyContext.Default.RuntimeLibraries.Any(library => library.Name.Equals(fileName, StringComparison.OrdinalIgnoreCase));
58-
59-
var assembly = (inCompileLibraries || inRuntimeLibraries)
60-
? Assembly.Load(new AssemblyName(fileName))
61-
: AssemblyLoadContext.Default.LoadFromAssemblyPath(assemblyPath);
62-
63-
return assembly;
64-
#else
65-
return Assembly.Load(new AssemblyName(fileName));
66-
#endif
67-
46+
return Assembly.Load(new AssemblyName(Path.GetFileNameWithoutExtension(assemblyPath)));
6847
}
6948
catch (Exception ex)
7049
{
71-
_log.Warning(string.Format(Resources.AssemblyLoadException, assemblyPath, ex.Message));
72-
73-
if (Log.IsTraceEnabled)
74-
{
75-
if (ex is ReflectionTypeLoadException reflection)
76-
{
77-
foreach (var exception in reflection.LoaderExceptions)
78-
{
79-
_log.Trace($"'{exception.Message}'.");
80-
}
81-
}
82-
else
83-
{
84-
_log.Trace($"{ex.GetType()}: '{ex.Message}'.");
85-
}
86-
}
50+
ExceptionRaised.Invoke(this, new ExceptionRaisedEventArgs($"GetAssembly({assemblyPath})",ex));
8751

8852
return null;
8953
}
@@ -260,27 +224,17 @@ public Type GetType(string typeName)
260224

261225
public IEnumerable<Type> GetTypes(Assembly assembly)
262226
{
227+
Guard.AgainstNull(assembly, nameof(assembly));
228+
263229
Type[] types;
264230

265231
try
266232
{
267-
_log.Trace(string.Format(Resources.TraceGetTypesFromAssembly, assembly));
268-
269233
types = assembly.GetTypes();
270234
}
271235
catch (Exception ex)
272236
{
273-
if (ex is ReflectionTypeLoadException reflection)
274-
{
275-
foreach (var exception in reflection.LoaderExceptions)
276-
{
277-
_log.Error($"'{exception.Message}'.");
278-
}
279-
}
280-
else
281-
{
282-
_log.Error($"{ex.GetType()}: '{ex.Message}'.");
283-
}
237+
ExceptionRaised.Invoke(this, new ExceptionRaisedEventArgs($"GetTypes({assembly.FullName})", ex));
284238

285239
return new List<Type>();
286240
}

Shuttle.Core.Reflection/ReflectionServiceExtensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System.Reflection;
44
using System.Text.RegularExpressions;
55
using Shuttle.Core.Contract;
6-
using Shuttle.Core.Logging;
76

87
namespace Shuttle.Core.Reflection
98
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
<ItemGroup>
2424
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="6.0.0" />
25-
<PackageReference Include="Shuttle.Core.Logging" Version="10.0.4" />
25+
<PackageReference Include="Shuttle.Core.Contract" Version="10.1.0" />
2626
</ItemGroup>
2727

2828
<ItemGroup>

Shuttle.Core.Reflection/TypeExtensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4-
using System.Net;
54
using System.Text.RegularExpressions;
65
using Shuttle.Core.Contract;
76

0 commit comments

Comments
 (0)