除非我先在该程序集中实例化一个类,否则无法对引用的程序集执行 Assembly.Load(String) 操作。怎么解决?
我这里有一个非常奇怪的问题。看起来除非我在程序集中实例化一个类,否则我会收到程序集未找到错误。
例如:
Assembly.Load("something.blah, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")
Type mqType = Type.GetType(query.Attribute(fullyQualifiedName + ", " + assemblyInfo);
Object mq = Activator.CreateInstance(mqType);
在 Assembly.Load 上引发 FileNotFound 异常
这:
Assembly.Load("something.blah, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")
new someClassInAssembly();
Type mqType = Type.GetType(query.Attribute(fullyQualifiedName + ", " + assemblyInfo);
Object mq = Activator.CreateInstance(mqType);
工作正常。是的,即使是在Assembly.Load之后实例化的,所以显然是编译时的问题。如何明确确保程序集已加载并且在运行时可找到,某处是否有编译设置,我需要做什么?
I have a very strange problem here. It looks like unless I instantiate a class within an assembly I get an assembly not found error.
For example:
Assembly.Load("something.blah, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")
Type mqType = Type.GetType(query.Attribute(fullyQualifiedName + ", " + assemblyInfo);
Object mq = Activator.CreateInstance(mqType);
Throws a FileNotFound exception on Assembly.Load
This:
Assembly.Load("something.blah, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")
new someClassInAssembly();
Type mqType = Type.GetType(query.Attribute(fullyQualifiedName + ", " + assemblyInfo);
Object mq = Activator.CreateInstance(mqType);
Works fine. Yes, even if it is instantiated after Assembly.Load, so it is clearly a problem during compilation. How do I explicitly make sure that the assembly is loaded and findable during runtime, is there a compilation setting somewhere, what do I need to do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通过提供路径,确保您正在加载您认为正在加载的程序集:
Make sure you're loading the assembly you think you're loading, by supplying the path:
老实说,如果它只是一个或几个引用,只需在某处添加显式引用即可,这样会节省您很多精力。
替代方案是将 dll 手动拖拽到正在运行的进程的 bin 中,或者将 dll 添加到 gac 中。我宁愿使用不太优雅的静态构造函数并继续。
Honestly, if its just a single reference or a handful, just add an explicit reference somewhere it will save you a lot of effort.
The alternatives are along the lines of hauling dlls manually to the bin of your running process or to add the dlls to the gac. I'd rather use the not-so-elegant static constructor and move on.