Loading assembly dynamically from a Typeprovider

倖福魔咒の 提交于 2019-12-11 20:29:21

问题


I'm trying to add a feature to a type provider I'm working on to allow the user to specify a type. with Since type providers cannot provide generic methods, it seems the only way to do it is to reference the assembly that has the type.

I've tried to make a proof of concept for this using a type from the Owin library, but I'm running into an issue when trying to use the provided type:

It says it cannot find the file, even though it obviously exists, or else the CSharpCodeProvider that I'm using would give an error (which it has done before for incorrect file paths). I've tried reproducing this problem in a seperate non-type-providing project, but it works there.

The code for this project is here (input-type branch): https://github.com/isaksky/routeprovider/tree/input-type

You can see the problem by opening the main RouteProvider Solution, and debugging doing the DebugOwin sample (it will open a new instance of visual studio for a sample solution that uses the RouteProvider).


回答1:


Dmitry's solution is one way to do it. The alternative is to simply make sure that your TP dependencies are located in the same folder that the TP itself resides in. We use this mechanism for the Azure Storage TP.




回答2:


Someone linked this answer, which has an explanation of the issue and a solution:

Type provider calling another dll in F#

I ended up having to use the typeprovider config (cfg)'s ReferencedAssemblies, like this:

      _assemblyResolver <- ResolveEventHandler(fun _ args ->
        let expectedName = (AssemblyName(args.Name)).Name + ".dll"
        let asmPath = 
            cfg.ReferencedAssemblies
            |> Seq.tryFind (fun asmPath -> IO.Path.GetFileName(asmPath) = expectedName)
        match asmPath with
        | Some f -> Assembly.LoadFrom f
        | None -> null)
      System.AppDomain.CurrentDomain.add_AssemblyResolve(_assemblyResolver)


来源:https://stackoverflow.com/questions/33884350/loading-assembly-dynamically-from-a-typeprovider

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!