除非我先在该程序集中实例化一个类,否则无法对引用的程序集执行 Assembly.Load(String) 操作。怎么解决?

发布于 2024-10-09 13:08:55 字数 733 浏览 4 评论 0原文

我这里有一个非常奇怪的问题。看起来除非我在程序集中实例化一个类,否则我会收到程序集未找到错误。

例如:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奢欲 2024-10-16 13:08:55

通过提供路径,确保您正在加载您认为正在加载的程序集:

AssemblyName an = AssemblyName.GetAssemblyName(filePath);
Assembly.Load(an);

Make sure you're loading the assembly you think you're loading, by supplying the path:

AssemblyName an = AssemblyName.GetAssemblyName(filePath);
Assembly.Load(an);
假装不在乎 2024-10-16 13:08:55

老实说,如果它只是一个或几个引用,只需在某处添加显式引用即可,这样会节省您很多精力。

//Use a static constructor somewhere appropriate. 
static someClass(){
   new AssemblyYouCareAbout.Object();
}

替代方案是将 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.

//Use a static constructor somewhere appropriate. 
static someClass(){
   new AssemblyYouCareAbout.Object();
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文