I have one of those annoying problems where something that used to work stopped working. Check out this code:
我有一个令人烦恼的问题,那些曾经工作的东西停止工作。看看这段代码:
Assembly _abc_assembly = Assembly.LoadFile(“c:\junk\abcabstract\bin\abc.dll”);
ABC.ContentAttribute attribute;
attribute = (ABC.ContentAttribute)_abc_assembly.CreateInstance("ABC.TextAttribute");
ContentAttribute is defined in the dll. Obviously, this should work. You should be able to cast an object to itself.
ContentAttribute在dll中定义。显然,这应该工作。您应该能够将对象强制转换为自身。
But it produces this error:
但它产生了这个错误:
alt text http://www.yart.com.au/*/compile1.png
替代文字http://www.yart.com.au/*/compile1.png
This bug is discussed here http://www.yoda.arachsys.com/csharp/plugin.html which is how I got even this far. From this post I gather that the class ContentAttribute is somehow ending up in ABC.DLL and the website project's DLL.
这个bug在这里讨论http://www.yoda.arachsys.com/csharp/plugin.html这就是我到目前为止的方式。从这篇文章我收集到,ContentAttribute类以某种方式结束于ABC.DLL和网站项目的DLL。
The website project I have looks like this:
我看到的网站项目如下:
alt text http://www.yart.com.au/*/compile2.png
替代文字http://www.yart.com.au/*/compile2.png
Now ContentAttribute is not in this project, it is in the dll ABC.DLL. You can see that as I have expanded every branch and the file ContentAttribute.cs is not there.
现在ContentAttribute不在这个项目中,它在dll ABC.DLL中。您可以看到,因为我已经扩展了每个分支,并且ContentAttribute.cs文件不存在。
Yet somehow it is ending up in the dll for the website creating a duplicating reference. ContentAttribute is somehow ending up in ABC.DLL and the website project's DLL.
然而不知何故,它最终在网站的dll中创建了一个重复的引用。 ContentAttribute以某种方式结束于ABC.DLL和网站项目的DLL。
Can anyone tell me:
谁能告诉我:
a) Why is ContentAttribute in two dlls? I didn’t think including a dll in a project forced that code into the projects DLL.
a)为什么ContentAttribute在两个dll中?我没想到在项目中包含一个dll强制将代码强加到项目DLL中。
b) How to stop it from happening?
b)如何阻止它发生?
By the way, I definitely don't want to change the website project into a website application if I can avoid it.
顺便说一下,如果我能避免的话,我绝对不想将网站项目改为网站应用程序。
Notes:
Deleting the temporary ASP.NET files does not work. As soon as I compile my website project they get recreated.
删除临时ASP.NET文件不起作用。一旦我编译我的网站项目,他们就会重新创建。
alt text http://www.yart.com.au/*/compile3.png
alt text http://www.yart.com.au/*/compile3.png
2 个解决方案
#1
It's a namespace collision. It doesnt know which ContentAttribute to use since it is finding 2 in different namespaces/assemblies.
这是命名空间冲突。它不知道要使用哪个ContentAttribute,因为它在不同的命名空间/程序集中找到2。
You may have an old copy of the DLL named differently. Delete your temporary ASP.Net directories and recompile.
您可能有一个名为不同的旧DLL副本。删除临时ASP.Net目录并重新编译。
To avoid in the future:
为了避免将来:
Use fully qualified names for your objects if you need to get it to work.
如果需要使用它,请为对象使用完全限定名称。
ABC.ContentAttribute ca = new ABC.ContentAttribute();
or if casting do the same
或者如果铸造做同样的事情
ABC.ContentAttribute ca =(ABC.ContentAttribute)ca2;
#2
ABC.DLL is referenced by your website and it becomes part of it (it is in Bin folder). ASP.NET compiles your website and ABC.DLL is placed in temp location (C:\Windows...\Temporary ASP.NET Filse...). It gets loaded by ASP.NET automatically. You are trying to load ABC.DLL manually from different location (D:\junk\abcabstract\bin\abc.dl). Two assemblies do not match hence you get the error.
ABC.DLL由您的网站引用,它成为它的一部分(它在Bin文件夹中)。 ASP.NET编译您的网站,ABC.DLL位于临时位置(C:\ Windows ... \ Temporary ASP.NET Filse ...)。它自动由ASP.NET加载。您正尝试从不同位置手动加载ABC.DLL(D:\ junk \ abcabstract \ bin \ abc.dl)。两个程序集不匹配,因此您收到错误。
To stop this from happening you have to rethink your plugin architecture I guess. Can you give more information?
为了阻止这种情况发生,你必须重新考虑你的插件架构。你能提供更多信息吗?
Update: Why don't you fix it like this:
更新:你为什么不这样解决它:
// 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
It's a namespace collision. It doesnt know which ContentAttribute to use since it is finding 2 in different namespaces/assemblies.
这是命名空间冲突。它不知道要使用哪个ContentAttribute,因为它在不同的命名空间/程序集中找到2。
You may have an old copy of the DLL named differently. Delete your temporary ASP.Net directories and recompile.
您可能有一个名为不同的旧DLL副本。删除临时ASP.Net目录并重新编译。
To avoid in the future:
为了避免将来:
Use fully qualified names for your objects if you need to get it to work.
如果需要使用它,请为对象使用完全限定名称。
ABC.ContentAttribute ca = new ABC.ContentAttribute();
or if casting do the same
或者如果铸造做同样的事情
ABC.ContentAttribute ca =(ABC.ContentAttribute)ca2;
#2
ABC.DLL is referenced by your website and it becomes part of it (it is in Bin folder). ASP.NET compiles your website and ABC.DLL is placed in temp location (C:\Windows...\Temporary ASP.NET Filse...). It gets loaded by ASP.NET automatically. You are trying to load ABC.DLL manually from different location (D:\junk\abcabstract\bin\abc.dl). Two assemblies do not match hence you get the error.
ABC.DLL由您的网站引用,它成为它的一部分(它在Bin文件夹中)。 ASP.NET编译您的网站,ABC.DLL位于临时位置(C:\ Windows ... \ Temporary ASP.NET Filse ...)。它自动由ASP.NET加载。您正尝试从不同位置手动加载ABC.DLL(D:\ junk \ abcabstract \ bin \ abc.dl)。两个程序集不匹配,因此您收到错误。
To stop this from happening you have to rethink your plugin architecture I guess. Can you give more information?
为了阻止这种情况发生,你必须重新考虑你的插件架构。你能提供更多信息吗?
Update: Why don't you fix it like this:
更新:你为什么不这样解决它:
// Assembly _abc_assembly = Assembly.LoadFile(“c:\junk\abcabstract\bin\abc.dll”);
// ContentAttribute attribute;
// attribute = (ContentAttribute)_abc_assembly.CreateInstance("ABC.TextAttribute");
ContentAttribute attribute = new ContentAttribute();