我有一个令人烦恼的问题,那些以前工作的东西停止了工作。 看看这段代码:
Assembly _abc_assembly = Assembly.LoadFile(“c:\junk\abcabstract\bin\abc.dll”);
ABC.ContentAttribute attribute;
attribute = (ABC.ContentAttribute)_abc_assembly.CreateInstance("ABC.TextAttribute");
ContentAttribute在dll中定义。 显然,这应该工作。您应该能够将对象强制转换为自身。
但它产生了这个错误:
alt text http://www.yart.com.au/stackoverflow/compile1.png
这里讨论了这个错误http://www.yoda.arachsys.com/csharp/plugin.html,这就是我到目前为止的方式。从这篇文章我收集到ContentAttribute类以某种方式结束于ABC.DLL 和网站项目的DLL。
我看到的网站项目如下:
alt text http://www.yart.com.au/stackoverflow/compile2.png
现在ContentAttribute不在这个项目中,它在dll ABC.DLL中。您可以看到,因为我已经扩展了每个分支,而且ContentAttribute.cs文件不存在。
然而不知何故,它最终会在网站的dll中创建一个重复的引用。 ContentAttribute以某种方式结束于ABC.DLL和网站项目的DLL。
任何人都可以告诉我:
a)为什么ContentAttribute在两个dll中?我没想到在项目中包含一个dll强制将代码强加到项目DLL中。
b)如何阻止它发生?
顺便说一句,如果可以避免的话,我绝对不想将网站项目改为网站应用程序。
注意:
删除临时ASP.NET文件不起作用。一旦我编译我的网站项目,他们就会重新创建。
答案 0 :(得分:0)
ABC.DLL由您的网站引用,它成为它的一部分(它位于Bin文件夹中)。 ASP.NET编译您的网站,ABC.DLL位于临时位置(C:\ Windows ... \ Temporary ASP.NET Filse ...)。它自动由ASP.NET加载。您正在尝试从不同位置手动加载ABC.DLL(D:\ junk \ abcabstract \ bin \ abc.dl)。两个程序集不匹配,因此您收到错误。
要阻止这种情况发生,我猜你必须重新考虑你的插件架构。你能提供更多信息吗?
<强>更新强>: 你为什么不这样解决它:
// Assembly _abc_assembly = Assembly.LoadFile(“c:\junk\abcabstract\bin\abc.dll”);
// ContentAttribute attribute;
// attribute = (ContentAttribute)_abc_assembly.CreateInstance("ABC.TextAttribute");
ContentAttribute attribute = new ContentAttribute();
答案 1 :(得分:0)
这是命名空间冲突。它不知道要使用哪个ContentAttribute,因为它在不同的命名空间/程序集中找到2。
您可能拥有不同名称的旧DLL副本。删除临时ASP.Net目录并重新编译。
以后要避免:
如果需要让对象使用,请使用完全限定名称。
ABC.ContentAttribute ca = new ABC.ContentAttribute();
或者如果施放相同的
ABC.ContentAttribute ca =(ABC.ContentAttribute)ca2;