我试图从程序集加载资源而不锁定dll文件。
我尝试以下操作,这与我之前在C#中使用的内容类似:
type IEmbeddedResourceReader =
abstract ReadResource : referencedAssemblies:string[] * asmName:string * resourceName:string -> string option
type EmbeddedResourceReader() =
inherit MarshalByRefObject()
interface IEmbeddedResourceReader with
member __.ReadResource(referencedAssemblies, asmName, resourceName) =
try
let asmLocation =
referencedAssemblies
|> Array.tryFind (fun (x:string) -> x.EndsWith(asmName + ".dll", StringComparison.InvariantCultureIgnoreCase))
let asm =
match asmLocation with
| Some asmLocation -> Assembly.LoadFrom asmLocation
| None -> Assembly.Load(asmName)
use sr = new StreamReader(asm.GetManifestResourceStream(resourceName))
Some(sr.ReadToEnd())
with _ ->
None
static member ReadResource(referencedAssemblies, resourceName:string) =
match resourceName.Split(',') with
| [| asmName; name |] ->
let domain = AppDomain.CreateDomain "Embedded Resource Reader"
try
let reader = domain.CreateInstanceFromAndUnwrap(Assembly.GetExecutingAssembly().Location,
typeof<EmbeddedResourceReader>.FullName) :?> IEmbeddedResourceReader
reader.ReadResource(referencedAssemblies, asmName.Trim(), name.Trim())
finally
AppDomain.Unload domain
| _ -> None
但是我收到了InvalidCastException(无法将透明代理强制转换为&#39; IEmbeddedResourceReader&#39;。)
我做错了什么?
答案 0 :(得分:1)
原来我需要添加它才能起作用:
static do
AppDomain.CurrentDomain.add_AssemblyResolve(ResolveEventHandler(fun _ args ->
if args.Name = typeof<IEmbeddedResourceReader>.Assembly.FullName then
typeof<IEmbeddedResourceReader>.Assembly
else
null))
感谢@mitekm(德米特里莫罗佐夫)的回答
不幸的是,dll不再被锁定,但xml和pdb文件仍被VS锁定:/