Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 13 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,16 @@ Assembly FindAssemblyNamed(string name)
Returns the `Assembly` that has the requested `name`; else `null`.

``` c#
IEnumerable<Assembly> GetAssemblies()
```

Returns all runtime assemblies as well as those in the `AppDomain.CurrentDomain.BaseDirectory` and `AppDomain.CurrentDomain.RelativeSearchPath`.

``` c#
IEnumerable<Assembly> GetAssemblies(string folder)
Assembly GetAssembly(string assemblyPath)
```

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

``` c#
Assembly GetAssembly(string assemblyPath)
```c#
IEnumerable<Assembly> GetAssemblies()
```

Returns the requested assembly if found; else `null`.
Returns all reachable assemblies.

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

``` c#
IEnumerable<Assembly> GetMatchingAssemblies(string regex, string folder)
IEnumerable<Assembly> GetRuntimeAssemblies()
```

Returns a collection of assemblies in the given `folder` that have their file name matching the given `regex` expression.
For .Net 4.6+ returns `AppDomain.CurrentDomain.GetAssemblies();`. For .Net Core 2.0+ all the `DependencyContext.Default.GetRuntimeAssemblyNames(RuntimeEnvironment.GetRuntimeIdentifier())` assembly names are resolved.

``` c#
IEnumerable<Assembly> GetRuntimeAssemblies()
Type GetType(string typeName)
```

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

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

``` c#
IEnumerable<Type> GetTypesAssignableTo<T>()
IEnumerable<Type> GetTypesAssignableTo(Type type)
IEnumerable<Type> GetTypesAssignableTo<T>(Assembly assembly)
IEnumerable<Type> GetTypesAssignableTo(Type type, Assembly assembly)
IEnumerable<Type> GetTypesAssignableTo<T>();
IEnumerable<Type> GetTypesAssignableTo(Type type);
IEnumerable<Type> GetTypesAssignableTo<T>(Assembly assembly);
IEnumerable<Type> GetTypesAssignableTo(Type type, Assembly assembly);
```

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.
Expand Down
6 changes: 4 additions & 2 deletions Shuttle.Core.Reflection/.package/package.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,26 @@
<package>
<metadata>
<id>Shuttle.Core.Reflection</id>
<version>11.0.3</version>
<version>12.0.0</version>
<authors>Eben Roux</authors>
<owners>Eben Roux</owners>
<license type="expression">BSD-3-Clause</license>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<icon>images\logo.png</icon>
<readme>docs\README.md</readme>
<repository type="git" url="https://github.com/shuttle/Shuttle.Core.Reflection.git" />
<projectUrl>https://github.com/shuttle/Shuttle.Core.Reflection</projectUrl>
<description>Reflection infrastructure components.</description>
<copyright>Copyright (c) 2022, Eben Roux</copyright>
<tags>shuttle reflection</tags>
<dependencies>
<dependency id="Microsoft.Extensions.DependencyModel" version="6.0.0" />
<dependency id="Shuttle.Core.Logging" version="10.0.4" />
<dependency id="Shuttle.Core.Contract" version="10.1.0" />
</dependencies>
</metadata>
<files>
<file src="..\..\..\.media\logo.png" target="images" />
<file src="..\..\..\README.md" target="docs\" />
<file src="lib\**\*.*" target="lib" />
</files>
</package>
2 changes: 2 additions & 0 deletions Shuttle.Core.Reflection/.package/package.nuspec.template
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<license type="expression">BSD-3-Clause</license>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<icon>images\logo.png</icon>
<readme>docs\README.md</readme>
<repository type="git" url="https://github.com/shuttle/Shuttle.Core.Reflection.git" />
<projectUrl>https://github.com/shuttle/Shuttle.Core.Reflection</projectUrl>
<description>Reflection infrastructure components.</description>
Expand All @@ -20,6 +21,7 @@
</metadata>
<files>
<file src="..\..\..\.media\logo.png" target="images" />
<file src="..\..\..\README.md" target="docs\" />
<file src="lib\**\*.*" target="lib" />
</files>
</package>
20 changes: 20 additions & 0 deletions Shuttle.Core.Reflection/ExceptionRaisedEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using Shuttle.Core.Contract;

namespace Shuttle.Core.Reflection
{
public class ExceptionRaisedEventArgs : EventArgs
{
public string MethodName { get; }
public Exception Exception { get; }

public ExceptionRaisedEventArgs(string methodName, Exception exception)
{
Guard.AgainstNullOrEmptyString(methodName, nameof(methodName));
Guard.AgainstNull(exception, nameof(exception));

MethodName = methodName;
Exception = exception;
}
}
}
2 changes: 2 additions & 0 deletions Shuttle.Core.Reflection/IReflectionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ namespace Shuttle.Core.Reflection
{
public interface IReflectionService
{
event EventHandler<ExceptionRaisedEventArgs> ExceptionRaised;

string AssemblyPath(Assembly assembly);
Assembly GetAssembly(string assemblyPath);
Assembly FindAssemblyNamed(string name);
Expand Down
4 changes: 2 additions & 2 deletions Shuttle.Core.Reflection/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
[assembly: AssemblyTitle(".NET Standard")]
#endif

[assembly: AssemblyVersion("11.0.3.0")]
[assembly: AssemblyVersion("12.0.0.0")]
[assembly: AssemblyCopyright("Copyright (c) 2022, Eben Roux")]
[assembly: AssemblyProduct("Shuttle.Core.Reflection")]
[assembly: AssemblyCompany("Eben Roux")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyInformationalVersion("11.0.3")]
[assembly: AssemblyInformationalVersion("12.0.0")]
[assembly: ComVisible(false)]
60 changes: 7 additions & 53 deletions Shuttle.Core.Reflection/ReflectionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
using System.Text.RegularExpressions;
using Microsoft.Extensions.DependencyModel;
using Shuttle.Core.Contract;
using Shuttle.Core.Logging;
#if NETCOREAPP
using System.Runtime.Loader;
#endif

namespace Shuttle.Core.Reflection
{
Expand All @@ -21,12 +17,9 @@ public class ReflectionService : IReflectionService
".exe"
};

private readonly ILog _log;

public ReflectionService()
public event EventHandler<ExceptionRaisedEventArgs> ExceptionRaised = delegate
{
_log = Log.For(this);
}
};

public string AssemblyPath(Assembly assembly)
{
Expand All @@ -50,40 +43,11 @@ public Assembly GetAssembly(string assemblyPath)

try
{
var fileName = Path.GetFileNameWithoutExtension(assemblyPath);

#if NETCOREAPP
var inCompileLibraries = DependencyContext.Default.CompileLibraries.Any(library => library.Name.Equals(fileName, StringComparison.OrdinalIgnoreCase));
var inRuntimeLibraries = DependencyContext.Default.RuntimeLibraries.Any(library => library.Name.Equals(fileName, StringComparison.OrdinalIgnoreCase));

var assembly = (inCompileLibraries || inRuntimeLibraries)
? Assembly.Load(new AssemblyName(fileName))
: AssemblyLoadContext.Default.LoadFromAssemblyPath(assemblyPath);

return assembly;
#else
return Assembly.Load(new AssemblyName(fileName));
#endif

return Assembly.Load(new AssemblyName(Path.GetFileNameWithoutExtension(assemblyPath)));
}
catch (Exception ex)
{
_log.Warning(string.Format(Resources.AssemblyLoadException, assemblyPath, ex.Message));

if (Log.IsTraceEnabled)
{
if (ex is ReflectionTypeLoadException reflection)
{
foreach (var exception in reflection.LoaderExceptions)
{
_log.Trace($"'{exception.Message}'.");
}
}
else
{
_log.Trace($"{ex.GetType()}: '{ex.Message}'.");
}
}
ExceptionRaised.Invoke(this, new ExceptionRaisedEventArgs($"GetAssembly({assemblyPath})",ex));

return null;
}
Expand Down Expand Up @@ -260,27 +224,17 @@ public Type GetType(string typeName)

public IEnumerable<Type> GetTypes(Assembly assembly)
{
Guard.AgainstNull(assembly, nameof(assembly));

Type[] types;

try
{
_log.Trace(string.Format(Resources.TraceGetTypesFromAssembly, assembly));

types = assembly.GetTypes();
}
catch (Exception ex)
{
if (ex is ReflectionTypeLoadException reflection)
{
foreach (var exception in reflection.LoaderExceptions)
{
_log.Error($"'{exception.Message}'.");
}
}
else
{
_log.Error($"{ex.GetType()}: '{ex.Message}'.");
}
ExceptionRaised.Invoke(this, new ExceptionRaisedEventArgs($"GetTypes({assembly.FullName})", ex));

return new List<Type>();
}
Expand Down
1 change: 0 additions & 1 deletion Shuttle.Core.Reflection/ReflectionServiceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Reflection;
using System.Text.RegularExpressions;
using Shuttle.Core.Contract;
using Shuttle.Core.Logging;

namespace Shuttle.Core.Reflection
{
Expand Down
2 changes: 1 addition & 1 deletion Shuttle.Core.Reflection/Shuttle.Core.Reflection.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="6.0.0" />
<PackageReference Include="Shuttle.Core.Logging" Version="10.0.4" />
<PackageReference Include="Shuttle.Core.Contract" Version="10.1.0" />
</ItemGroup>

<ItemGroup>
Expand Down
1 change: 0 additions & 1 deletion Shuttle.Core.Reflection/TypeExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using Shuttle.Core.Contract;

Expand Down