Repository navigation
Expand file tree
/
Copy pathSingleRootTypeProvider.fs
More file actions
56 lines (49 loc) · 2.62 KB
/
Copy pathSingleRootTypeProvider.fs
File metadata and controls
56 lines (49 loc) · 2.62 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
namespace ProviderImplementation.ProvidedTypes
open FSharp.Data.SqlClient
open Microsoft.FSharp.Core.CompilerServices
open System.Reflection
open System
[<AbstractClass>]
[<CompilerMessageAttribute("This API supports the FSharp.Data.SqlClient infrastructure and is not intended to be used directly from your code.", 101, IsHidden = true)>]
type SingleRootTypeProvider(config: TypeProviderConfig, providerName, parameters, ?isErased) as this =
inherit TypeProviderForNamespaces (
config
#if MICROSOFT_DATA_SQLCLIENT
, assemblyReplacementMap=[("FSharp.Data.MicrosoftSqlClient.DesignTime", "FSharp.Data.MicrosoftSqlClient")]
#endif
#if SYSTEM_DATA_SQLCLIENT
, assemblyReplacementMap=[("FSharp.Data.SqlClient.DesignTime", "FSharp.Data.SqlClient")]
#endif
, addDefaultProbingLocation=true
)
let cache = new Cache<ProvidedTypeDefinition>()
do
FixReferenceAssemblies.loadSqlServerTypes.Force() |> Option.iter this.TargetContext.AddSourceAssembly
do
let isErased = defaultArg isErased true
let nameSpace = this.GetType().Namespace
let assembly = Assembly.GetExecutingAssembly()
let _ = FixReferenceAssemblies.manualLoadNet8Runtime.Force()
let providerType = ProvidedTypeDefinition(assembly, nameSpace, providerName, Some typeof<obj>, hideObjectMethods = true, isErased = isErased)
providerType.DefineStaticParameters(
parameters = parameters,
instantiationFunction = fun typeName args ->
match cache.TryGetValue(typeName) with
| true, cachedType -> cachedType.Value
| false, _ ->
let typ, monitors = this.CreateRootType(assembly, nameSpace, typeName, args)
monitors
|> Seq.iter(fun m ->
match m with
| :? System.Runtime.Caching.ChangeMonitor as monitor ->
monitor.NotifyOnChanged(fun _ ->
cache.Remove(typeName)
this.Invalidate()
monitor.Dispose()
)
| _ -> ()
)
cache.GetOrAdd(typeName, typ)
)
this.AddNamespace( nameSpace, [ providerType ])
abstract CreateRootType: assemblyName: Assembly * nameSpace: string * typeName: string * args: obj[] -> Lazy<ProvidedTypeDefinition> * obj[] // ChangeMonitor[] underneath but there is a problem https://github.com/fsprojects/FSharp.Data.SqlClient/issues/234#issuecomment-240694390