-
Notifications
You must be signed in to change notification settings - Fork 871
Expand file tree
/
Copy pathAssemblyResolveHandler.fs
More file actions
119 lines (86 loc) · 4.61 KB
/
Copy pathAssemblyResolveHandler.fs
File metadata and controls
119 lines (86 loc) · 4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
namespace FSharp.Compiler.DependencyManager
open System
open System.IO
open System.Reflection
open Internal.Utilities.FSharpEnvironment
open Internal.Utilities.Library
/// Signature for ResolutionProbe callback
/// host implements this, it's job is to return a list of assembly paths to probe.
type AssemblyResolutionProbe = delegate of Unit -> seq<string>
/// Type that encapsulates AssemblyResolveHandler for managed packages
type AssemblyResolveHandlerCoreclr(assemblyProbingPaths: AssemblyResolutionProbe option) as this =
let loadContextType =
!!Type.GetType("System.Runtime.Loader.AssemblyLoadContext, System.Runtime.Loader", false)
let loadFromAssemblyPathMethod =
!!loadContextType.GetMethod("LoadFromAssemblyPath", [| typeof<string> |])
let eventInfo = !!loadContextType.GetEvent("Resolving")
let handler, defaultAssemblyLoadContext =
let ti = typeof<AssemblyResolveHandlerCoreclr>
let gmi =
!!ti.GetMethod("ResolveAssemblyNetStandard", BindingFlags.Instance ||| BindingFlags.NonPublic)
let mi = gmi.MakeGenericMethod(loadContextType)
let del = Delegate.CreateDelegate(!!eventInfo.EventHandlerType, this, mi)
let prop =
(!!loadContextType.GetProperty("Default", BindingFlags.Static ||| BindingFlags.Public)).GetValue(null, null)
del, prop
do eventInfo.AddEventHandler(defaultAssemblyLoadContext, handler)
member _.ResolveAssemblyNetStandard (ctxt: 'T) (assemblyName: AssemblyName) : Assembly =
let loadAssembly path =
!!loadFromAssemblyPathMethod.Invoke(ctxt, [| path |]) :?> Assembly
let assemblyPaths =
match assemblyProbingPaths with
| None -> Seq.empty<string>
| Some assemblyProbingPaths -> assemblyProbingPaths.Invoke()
try
// args.Name is a displayname formatted assembly version.
// E.g: "System.IO.FileSystem, Version=4.1.1.0, Culture=en-US, PublicKeyToken=b03f5f7f11d50a3a"
let simpleName = assemblyName.Name
let assemblyPathOpt =
assemblyPaths
|> Seq.tryFind (fun path -> String.Equals(Path.GetFileNameWithoutExtension(path), simpleName))
match assemblyPathOpt with
| Some path -> loadAssembly path
| None -> Unchecked.defaultof<Assembly>
with _ ->
Unchecked.defaultof<Assembly>
interface IDisposable with
member _x.Dispose() =
eventInfo.RemoveEventHandler(defaultAssemblyLoadContext, handler)
/// Type that encapsulates AssemblyResolveHandler for managed packages
type AssemblyResolveHandlerDeskTop(assemblyProbingPaths: AssemblyResolutionProbe option) =
let resolveAssemblyNET (assemblyName: AssemblyName) : Assembly =
let assemblyPaths =
match assemblyProbingPaths with
| None -> Seq.empty<string>
| Some assemblyProbingPaths -> assemblyProbingPaths.Invoke()
try
// args.Name is a displayname formatted assembly version.
// E.g: "System.IO.FileSystem, Version=4.1.1.0, Culture=en-US, PublicKeyToken=b03f5f7f11d50a3a"
let simpleName = assemblyName.Name
let assemblyPathOpt =
assemblyPaths
|> Seq.tryFind (fun path -> String.Equals(Path.GetFileNameWithoutExtension(path), simpleName))
match assemblyPathOpt with
| Some path -> Assembly.LoadFrom path
| None -> Unchecked.defaultof<Assembly>
with _ ->
Unchecked.defaultof<Assembly>
let handler =
ResolveEventHandler(fun _ (args: ResolveEventArgs) -> resolveAssemblyNET (AssemblyName(args.Name)))
do AppDomain.CurrentDomain.add_AssemblyResolve handler
interface IDisposable with
member _x.Dispose() =
AppDomain.CurrentDomain.remove_AssemblyResolve handler
type AssemblyResolveHandler internal (assemblyProbingPaths: AssemblyResolutionProbe option) =
let handler =
assemblyProbingPaths
|> Option.map (fun _ ->
if isRunningOnCoreClr then
new AssemblyResolveHandlerCoreclr(assemblyProbingPaths) :> IDisposable
else
new AssemblyResolveHandlerDeskTop(assemblyProbingPaths) :> IDisposable)
new(assemblyProbingPaths: AssemblyResolutionProbe | null) = new AssemblyResolveHandler(Option.ofObj assemblyProbingPaths)
interface IDisposable with
member _.Dispose() =
handler |> Option.iter (fun handler -> handler.Dispose())