Tuesday, March 27, 2012

Self-Note: Make type provider work in FSX file

When I wrote the type provider using an my own erased type (not system type) and wanted to to test it in the FSX file, I run into an error. What I did is to write my type in a separate DLL and reference that DLL in the FSX file, which is used for testing purpose, and in the type provider file. 

The error I got in the FSX file is: cannot reference to the DLL contains my own type. I double checked the FSX and I did add #r. The real reason behind is the AssemblyLoad context. The type provider code was doing the Assembly.LoadFile in a different load context. The following code should resolve the problem:

    static do
        System.AppDomain.CurrentDomain.add_AssemblyResolve(fun sender anea ->
            let loaded = System.AppDomain.CurrentDomain.GetAssemblies()
            let an = System.Reflection.AssemblyName(anea.Name)
            match loaded |> Seq.tryFind(fun a -> System.Reflection.AssemblyName.ReferenceMatchesDefinition(an, a.GetName())) with
                | Some a -> a
                | None -> null

No comments: