-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
140 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using System.Threading; | ||
|
||
namespace CK.Core | ||
{ | ||
|
||
/// <summary> | ||
/// Assemby loader helper: hooks the <see cref="AppDomain.AssemblyResolve"/> event | ||
/// in order to try to load a version-less assembly. | ||
/// The <see cref="GetResolvedList"/> is maintained and can be used to diagnose any assembly binding issues. | ||
/// All members are thread safe. | ||
/// </summary> | ||
public static class WeakAssemblyNameResolver | ||
{ | ||
static int _installCount; | ||
static List<KeyValuePair<AssemblyName, AssemblyName>> _list = new List<KeyValuePair<AssemblyName, AssemblyName>>(); | ||
|
||
/// <summary> | ||
/// Gets whether this helper is active. | ||
/// </summary> | ||
public static bool IsInstalled => _installCount >= 0; | ||
|
||
/// <summary> | ||
/// Installs the hook if not already installed. | ||
/// Instead of using Install/<see cref="Uninstall"/>, the <see cref="TemporaryInstall"/> helper should be used. | ||
/// </summary> | ||
public static void Install() | ||
{ | ||
if( Interlocked.Increment( ref _installCount ) == 1 ) | ||
{ | ||
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Uninstall the hook if possible. | ||
/// </summary> | ||
public static void Uninstall() | ||
{ | ||
if( Interlocked.Decrement( ref _installCount ) == 0 ) | ||
{ | ||
AppDomain.CurrentDomain.AssemblyResolve -= CurrentDomain_AssemblyResolve; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Gets the pair of requested name and eventually resolved name that have been resolved so far. | ||
/// </summary> | ||
/// <returns>A copy of the internally maintained list of resolved assembly names.</returns> | ||
public static KeyValuePair<AssemblyName, AssemblyName>[] GetResolvedList() | ||
{ | ||
lock( _list ) | ||
{ | ||
return _list.ToArray(); | ||
} | ||
} | ||
|
||
class Auto : IDisposable | ||
{ | ||
bool _done; | ||
|
||
public void Dispose() | ||
{ | ||
if( !_done ) | ||
{ | ||
_done = true; | ||
Uninstall(); | ||
} | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Temporary installs the hook that will be uninstalled when the returned object will be disposed. | ||
/// </summary> | ||
/// <returns>The dispoable to dispose when done.</returns> | ||
public static IDisposable TemporaryInstall() | ||
{ | ||
Install(); | ||
return new Auto(); | ||
} | ||
|
||
static Assembly CurrentDomain_AssemblyResolve( object sender, ResolveEventArgs args ) | ||
{ | ||
var failed = new AssemblyName( args.Name ); | ||
var resolved = failed.Version != null && string.IsNullOrWhiteSpace( failed.CultureName ) | ||
? Assembly.Load( new AssemblyName( failed.Name ) ) | ||
: null; | ||
lock( _list ) | ||
{ | ||
_list.Add( new KeyValuePair<AssemblyName, AssemblyName>( failed, resolved?.GetName() ) ); | ||
} | ||
return resolved; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<packages> | ||
<package id="Cake.Common" version="0.22.0" targetFramework="net461" developmentDependency="true" /> | ||
<package id="Cake.Core" version="0.22.0" targetFramework="net461" /> | ||
<package id="CK.Text" version="6.0.1" targetFramework="net461" /> | ||
<package id="Code.Cake" version="0.15.0" targetFramework="net451" /> | ||
<package id="CSemVer" version="1.0.1" targetFramework="net461" /> | ||
<package id="Cake.Common" version="0.23.0" targetFramework="net461" developmentDependency="true" /> | ||
<package id="Cake.Core" version="0.23.0" targetFramework="net461" /> | ||
<package id="CK.Text" version="6.1.0" targetFramework="net461" /> | ||
<package id="Code.Cake" version="0.18.0" targetFramework="net461" /> | ||
<package id="CSemVer" version="1.1.0" targetFramework="net461" /> | ||
<package id="NUnit.Runners.Net4" version="2.6.4" targetFramework="net461" /> | ||
<package id="SimpleGitVersion.Cake" version="0.28.0" targetFramework="net461" developmentDependency="true" /> | ||
<package id="SimpleGitVersion.Core" version="0.28.0" targetFramework="net461" developmentDependency="true" /> | ||
<package id="SimpleGitVersion.Cake" version="0.30.0" targetFramework="net461" developmentDependency="true" /> | ||
<package id="SimpleGitVersion.Core" version="0.30.0" targetFramework="net461" developmentDependency="true" /> | ||
</packages> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<configuration> | ||
<packageSources> | ||
<clear /> | ||
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" /> | ||
<add key="Invenietis - Preview" value="https://www.myget.org/F/invenietis-preview/api/v3/index.json" protocolVersion="3" /> | ||
<add key="Invenietis - CI" value="https://www.myget.org/F/invenietis-ci/api/v3/index.json" protocolVersion="3" /> | ||
<clear /> | ||
<add key="Invenietis - Release" value="https://www.myget.org/F/invenietis-release/api/v3/index.json" protocolVersion="3" /> | ||
<add key="Invenietis - Preview" value="https://www.myget.org/F/invenietis-preview/api/v3/index.json" protocolVersion="3" /> | ||
<add key="Invenietis - CI" value="https://www.myget.org/F/invenietis-ci/api/v3/index.json" protocolVersion="3" /> | ||
</packageSources> | ||
</configuration> | ||
</configuration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,14 @@ | ||
{ | ||
"profiles": { | ||
"net461": { | ||
"NUnit Debug": { | ||
"commandName": "Executable", | ||
"executablePath": "..\\..\\packages\\NUnit.Runners.Net4.2.6.4\\tools\\nunit.exe", | ||
"commandLineArgs": "bin\\Debug\\net461\\CK.Core.Tests.dll" | ||
}, | ||
"NUnit Release": { | ||
"commandName": "Executable", | ||
"executablePath": "..\\..\\packages\\NUnit.Runners.Net4.2.6.4\\tools\\nunit.exe", | ||
"commandLineArgs": "bin\\Release\\net461\\CK.Core.Tests.dll" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters