从Typeprovider动态加载程序集 [英] Loading assembly dynamically from a Typeprovider
问题描述
我正在尝试向正在使用的类型提供程序中添加功能,以允许用户指定类型.由于类型提供程序无法提供泛型方法,因此似乎唯一的方法是引用具有该类型的程序集.
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.
我尝试使用Owin库中的类型对此进行概念验证,但是在尝试使用提供的类型时遇到了问题:
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:
它说即使明显存在它也找不到文件,否则我正在使用的CSharpCodeProvider
会给出一个错误(对于不正确的文件路径,以前已经这样做了).我曾尝试在一个单独的非类型提供项目中重现此问题,但该方法在此可行.
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.
此项目的代码在这里(输入类型分支): https://github.com/isaksky/routeprovider/tree/input-type
The code for this project is here (input-type branch): https://github.com/isaksky/routeprovider/tree/input-type
您可以通过打开主RouteProvider解决方案并调试DebugOwin示例来看到问题(它将为使用RouteProvider的示例解决方案打开Visual Studio的新实例).
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).
推荐答案
有人链接了此答案,其中包含对该问题的说明和解决方案:
Someone linked this answer, which has an explanation of the issue and a solution:
我最终不得不使用typeprovider配置(cfg
)的ReferencedAssemblies
,如下所示:
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)
这篇关于从Typeprovider动态加载程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!