简体   繁体   English

防止在工作目录中加载dll,InvalidCastException类型A不能强制转换为类型B

[英]Preventing loading dll in working directory, InvalidCastException Type A cannot be cast to Type B

Is it possible to prevent the loading of a dll that is in the working directory and force it to use a dll somewhere else that I specify? 是否可以防止加载工作目录中的dll并迫使其在我指定的其他地方使用dll?

I am trying to run some unit tests and am getting this InvalidCast exception: 我正在尝试运行一些单元测试,并收到此InvalidCast异常:

[A]AssemblyA.DataType cannot be cast to [B]AssemblyA.DataType. [A] AssemblyA.DataType无法转换为[B] AssemblyA.DataType。 Type A originates from 'AssemblyA, Version=1.0.4645.21698, Culture=neutral, PublicKeyToken=null' in the context 'LoadFrom' at location 'E:\\webservice\\bin\\AssemblyA\\AssemblyA.dll'. 类型A源自位置'E:\\ webservice \\ bin \\ AssemblyA \\ AssemblyA.dll'的上下文'LoadFrom'中的'AssemblyA,Version = 1.0.4645.21698,Culture = neutral,PublicKeyToken = null'。 Type B originates from 'AssemblyA, Version=1.0.4645.21698, Culture=neutral, PublicKeyToken=null' in the context 'Default' at location 'E:\\TestResults\\Out\\AssemblyA.dll'. 类型B源自位置'E:\\ TestResults \\ Out \\ AssemblyA.dll'的上下文'Default'中的'AssemblyA,Version = 1.0.4645.21698,Culture = neutral,PublicKeyToken = null'。

On test startup, it calls some initialization code that uses Assembly.LoadFrom to load a dll from E:\\webservice\\bin\\AssemblyA\\AssemblyA.dll and create some of the types and put them in a cache. 在测试启动时,它将调用一些初始化代码,这些代码使用Assembly.LoadFrom从E:\\ webservice \\ bin \\ AssemblyA \\ AssemblyA.dll加载dll,并创建一些类型并将其放入缓存中。

There are post build events that xcopy the dlls to that location, so they are exactly the same dll in both locations. 有构建后事件将dll复制到该位置,因此它们在两个位置都是完全相同的dll。

But the unit test is running from the working directory of E:\\testresults\\out\\ so it automatically loads the AssemblyA.dll there, ignoring the assembly already loaded, without ever giving me a chance to intercept that load with the Appdomain.AssemblyResolve event 但是单元测试是从E:\\ testresults \\ out \\的工作目录运行的,因此它会自动将那里的AssemblyA.dll加载到该目录中,而忽略已加载的程序集,而没有给我机会用Appdomain.AssemblyResolve事件拦截该加载

So when the unit test code tries to retrieve an object from the cache that was put there from the E:\\webservice\\bin\\AssemblyA\\AssemblyA.dll it says invalid cast because its expecting the type from E:\\TestResults\\Out\\AssemblyA.dll 因此,当单元测试代码尝试从E:\\ webservice \\ bin \\ AssemblyA \\ AssemblyA.dll放入的缓存中检索对象时,它表示无效的转换,因为它期望从E:\\ TestResults \\ Out \\ AssemblyA获得类型.dll

After looking over Best Practices for Assembly Loading it seems like there is no way to prevent it from loading the dll from the working directory, even though I already manually loaded the dll I want to use. 在查看了“程序集加载的最佳实践”之后,即使我已经手动加载了要使用的dll,似乎也无法阻止它从工作目录中加载dll。 Are there any options besides changing the application to not load from E:\\webservice\\bin... at all? 除了将应用程序更改为完全不从E:\\ webservice \\ bin ...加载之外,还有其他选择吗?

(Using LoadFile or Load byte[] both also result in the same error) (同时使用LoadFile或Load byte []也会导致相同的错误)

Now, I try to help you out of you problem.. 现在,我尽力帮助您解决问题。

I recommended to sign your assembly with a StrongName . 我建议使用StrongName对您的程序集进行签名。

The CLR use this to identify the assembly. CLR使用它来标识程序集。 This is not almost solving your problem. 这几乎不能解决您的问题。 But to solve your problem I suggest to use an interface to encapsulate your diffrent types. 但是要解决您的问题,我建议使用一个接口来封装您的不同类型。 So, You can store any type in your cache and return only the intrerface, and the cast is gone. 因此,您可以将任何类型存储在缓存中,并且仅返回界面,而类型转换就消失了。

If this is not solving your problem, you must provide me more information. 如果这不能解决您的问题,则必须向我提供更多信息。

As Panos suggested in the comment above, this was solved by setting CopyLocal=false on the referenced assemblies. 正如Panos在上面的注释中所建议的那样,这是通过在引用的程序集上设置CopyLocal = false来解决的。 That way the CLR can't attempt to load the dll from the working directory because the dll isn't there. 这样,CLR无法尝试从工作目录中加载dll,因为dll不存在。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 加载序列化数据时,“ InvalidCastException:无法从源类型转换为目标类型。” - “InvalidCastException: Cannot cast from source type to destination type.” when loading serialized data 无法将类型A DLL强制转换为类型B DLL。 类型A起源于上下文。 - Type A DLL cannot be cast to type B DLL. Type A originates.. from in the context LoadFrom InvalidCastException:无法从源类型转换为目标类型。 sqlite - InvalidCastException: Cannot Cast from source type to destination type. sqlite 反序列化错误:InvalidCastException:无法从源类型转换为目标类型 - Deserialization Error: InvalidCastException: Cannot cast from source type to destination type C#Type A无法转换为B类(InvalidCastException)...上下文地狱? - C# Type A cannot be casted to Type B ( InvalidCastException)… Context hell? InvalidCastException:无法使用Unity(C#)从源类型转换为目标类型 - InvalidCastException: Cannot cast from source type to destination type, using Unity (C#) InvalidCastException:无法从源类型转换为目标类型。 (Unity C#,foreach列表循环) - InvalidCastException: Cannot cast from source type to destination type. (Unity c# ,foreach list loop) unity C# InvalidCastException:无法从源类型转换为目标类型 - Unity C# InvalidCastException: Cannot cast from source type to destination type System.InvalidCastException:无法转换类型的对象 - System.InvalidCastException: Unable to cast object of type 显式转换为已定义的类型会抛出`InvalidCastException` - Explicit cast to defined type throws `InvalidCastException`
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM