Tuesday, July 10, 2012

Self Note:Type Provider Assembly Resolve

thanks to my co-worker, Vlad, i can solve the type provider type assembly resolve problem. 

the code is below.

// tpc :TypeProviderConfig argument exposes enviroment-related information to the type provider
[< Microsoft.FSharp.Core.CompilerServices.TypeProvider >]
type TP(tpc : Microsoft.FSharp.Core.CompilerServices.TypeProviderConfig) =
    inherit TypeProviderForNamespaces()
    let handler = System.ResolveEventHandler(fun _ args ->
        let asmName = AssemblyName(args.Name)
        // assuming that we reference only dll files
        let expectedName = asmName.Name + ".dll"
        let expectedLocation =
            // we expect to find this assembly near the dll with type provider
            let d = IO.Path.GetDirectoryName(tpc.RuntimeAssembly)
            IO.Path.Combine(d, expectedName)
        if IO.File.Exists expectedLocation then Assembly.LoadFrom expectedLocation else null
    do System.AppDomain.CurrentDomain.add_AssemblyResolve handler
    interface System.IDisposable with
        member this.Dispose() = System.AppDomain.CurrentDomain.remove_AssemblyResolve handler

No comments: