简体   繁体   English

Visual Studio在哪里查找程序集?

[英]Where does Visual Studio look for assemblies?

I've got a framework which consists of many base classes that can be derived to develop many apps. 我有一个框架,其中包含许多基类,可以派生它们来开发许多应用程序。 Among these classes there is a subclass of System.Windows.Forms.Panel for which I wrote its own designer. 在这些类中有一个System.Windows.Forms.Panel的子类,我为它编写了自己的设计器。 Everything is working fine with Visual Studio 2005, but something goes wrong when I try to move to VS2010. Visual Studio 2005一切正常,但当我尝试迁移到VS2010时出现问题。 This is a much simplified version of what I am doing: 这是我正在做的简化版本:

I have a project called CoreClasse which contains an interface and two classes: 我有一个名为CoreClasse的项目,它包含一个接口和两个类:

public interface IConf
{
    string foo { get; set; }
    void InitFoo();
}

public class SimpleClass
{
    public string foo;
}

public class ConfLoader
{
    public static IConf LoadConf()
    {
        AssemblyName anAssemblyName = new AssemblyName("ConfClasses");
        Assembly anAssembly = Assembly.Load(anAssemblyName);
        IConf result = (IConf)anAssembly.CreateInstance("ConfClasses.ConfClass");
        result.InitFoo();
        return result;
    }
}

Then there is a project ConfClasses which references CoreClasses and contains just one class implementing IConf: 然后有一个项目ConfClasses引用CoreClasses并且只包含一个实现IConf的类:

public class ConfClass : IConf
{
    public SimpleClass confVal;

    public string foo
    {
        get { return confVal.foo; }
        set { confVal.foo = value; }
    }

    public void InitFoo()
    {
        confVal = new SimpleClass();
        confVal.foo = "bar";
    }
}

And finally there is a project for the controls which references only CoreClasses and contains a subclass of Panel and the associated designer: 最后有一个控件项目,它只引用CoreClasses并包含Panel的子类和相关的设计器:

[Designer("MyControls.Design.SimplePanelDesigner", typeof(IRootDesigner))]
public class SimplePanel : Panel
{
    public SimpleClass dummy = new SimpleClass();
}

public class SimplePanelDesigner : DocumentDesigner
{
    public IConf DesignerConf;

    public SimplePanelDesigner()
        : base()
    {
        DesignerConf = ConfLoader.LoadConf();
    }
}

Now I create another solution which references all these dlls and contains an empty subclass of SimplePanel. 现在我创建另一个引用所有这些dll的解决方案,并包含一个SimplePanel的空子类。 When I double click on this class in SolutionExplorer the constructor of SimplePanelDesigner is executed and the method LoadConf of ConfLoader is called. 当我在SolutionExplorer中双击此类时,将执行SimplePanelDesigner的构造函数并调用ConfLoader的方法LoadConf。 This means that ConfClasses.dll is loaded dinamically and an instance of ConfClass is created. 这意味着ConfClasses.dll以dinamically方式加载,并创建一个ConfClass实例。 Everything is fine up to this moment, but when InitFoo is called this exception is raised: 到目前为止一切都很好,但是当调用InitFoo时会引发异常:

Could not load file or assembly 'CoreClasses, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. 无法加载文件或程序集“CoreClasses,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null”或其依赖项之一。 The system cannot find the file specified. 该系统找不到指定的文件。

To make things harder, the exception is not actually raisedin this example, but this is exactly the kind of istructions my real app is executing, and the exception I am getting. 为了使事情变得更难,在这个例子中实际上没有提出异常,但这正是我真正的应用程序正在执行的那种结构,以及我得到的异常。 I haven't got a clue of what's happening here. 我还没有弄清楚这里发生了什么。 VS is executing a method which IS in CoreClasses. VS正在执行CoreClasses中的IS方法。 Why is it trying to load it again? 为什么要再次加载它? And where is it looking for it? 它在哪里寻找它? I checked the current AppDomain, too, but it has CoreClasses among its loaded assemblies, and it does not seem to change. 我也检查了当前的AppDomain,但它在其加载的程序集中有CoreClasses,它似乎没有改变。

Just to add some more details, every project is build in a common folder (not the usual obj/debug folder inside the project folder), and there is no other copy of my dlls on the PC at the moment I start my test. 只是为了添加更多细节,每个项目都构建在一个公共文件夹中(不是项目文件夹中常用的obj / debug文件夹),而且在我开始测试时,PC上没有其他的dll副本。 Then a copy of all the referenced dlls is done in a series of folders in the AppData\\Local\\Microsoft\\VisualStudio\\10.0\\ProjectAssemblies folder of my userprofile, and this seems to be the place VS is looking for the assemblies when Assembly.Load is executed, and I can find a copy of CoreClasses there. 然后所有引用的dll的副本在我的userprofile的AppData \\ Local \\ Microsoft \\ VisualStudio \\ 10.0 \\ ProjectAssemblies文件夹中的一系列文件夹中完成,这似乎是VS在Assembly.Load中查找程序集的地方执行,我可以在那里找到CoreClasses的副本。 I tried to clean all the folders, to rebuild everything, and to keep the different solutions opened/closed in every combination, but without any improvement. 我试图清理所有文件夹,重建所有内容,并在每个组合中保持打开/关闭不同的解决方案,但没有任何改进。

EDIT: 编辑:

As GranMasterFlush suggested, this is the FusionLog generated by the exception: 正如GranMasterFlush建议的那样,这是由异常生成的FusionLog:

=== Pre-bind state information ===
LOG: User = FCDB\fc0107
LOG: DisplayName = XEngine.Core, Version=1.0.0.1, Culture=neutral, PublicKeyToken=null (Fully-specified)
LOG: Appbase = file:///C:/Program Files (x86)/Microsoft Visual Studio 10.0/Common7/IDE/
LOG: Initial PrivatePath = NULL
Calling assembly : (Unknown).
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\devenv.exe.Config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: The same bind was seen before, and was failed with hr = 0x80070002.
ERR: Unrecoverable error occurred during pre-download check (hr = 0x80070002).

EDIT 2 编辑2

Just to add some info, I took a look at the fusion logs generated by my simple example and found out that exactly the same log has been generated while trying to load CoreClasses, but someway VisualStudio finds a way to cope with it. 只是为了添加一些信息,我看了一下我的简单例子生成的融合日志,发现在尝试加载CoreClasses时生成了完全相同的日志,但是VisualStudio找到了解决它的方法。

I've just found out what's happening. 我刚刚发现了正在发生的事情。 The difference between the simple example and the real thing is the fact that there is an AddIn involved. 简单示例和真实事物之间的区别在于涉及AddIn。 It's quite a log story, but this is it. 这是一个相当日志的故事,但就是这样。
As you can see from the code sample, I load the ConfClasses dll via reflection in order to avoid to add a reference to it. 从代码示例中可以看出,我通过反射加载ConfClasses dll,以避免添加对它的引用。 This is fine at runtime, but the designer complains saying that it's not able to cast IConf to IConf. 这在运行时很好,但设计师抱怨说它无法将IConf转换为IConf。 This happens because CoreClasses.dll is loaded from AppData\\Local\\Microsoft\\VisualStudio\\10.0\\ProjectAssemblies when the designer starts, but ConfClasses.dll is loaded from my bin folder and so are its references, so there are two versions of CoreClasses.dll and different versions of IConf. 发生这种情况是因为CoreClasses.dll是在设计器启动时从AppData \\ Local \\ Microsoft \\ VisualStudio \\ 10.0 \\ ProjectAssemblies加载的,但ConfClasses.dll是从我的bin文件夹加载的,因此它的引用也是如此,因此有两个版本的CoreClasses.dll和不同版本的IConf。
To bypass the problem I developed an AddIn that, when an assembly is loaded with Assembly.Load at design time, adds a reference to that assembly, and then cleans the references when the last designer window is closed. 为了绕过这个问题,我开发了一个AddIn,当在设计时使用Assembly.Load加载程序集时,添加对该程序集的引用,然后在关闭最后一个设计器窗口时清除引用。
Everything was OK with VS2005, but using ProcMon.exe I found out that VS2010 added a new folder where the addins look for assemblies: Program Files (x86)\\Microsoft Visual Studio 10.0\\Common7\\IDE\\CommonExtensions\\DataDesign VS2005一切正常,但使用ProcMon.exe我发现VS2010添加了一个新文件夹,其中插件寻找程序集: Program Files(x86)\\ Microsoft Visual Studio 10.0 \\ Common7 \\ IDE \\ CommonExtensions \\ DataDesign
I copied my assembly there and everything works again. 我在那里复制了我的装配,一切都恢复了。 Now it's only a matter of finding a way to add things there manually. 现在只需要找到一种手动添加东西的方法。

Have you tried using the Assembly Binding Log Viewer to investigate why this failure to load the assemblies? 您是否尝试使用程序集绑定日志查看器来调查加载程序集失败的原因?

http://msdn.microsoft.com/en-us/library/e74a18c4%28v=vs.71%29.aspx http://msdn.microsoft.com/en-us/library/e74a18c4%28v=vs.71%29.aspx

There's another article here about using it: 这里有另一篇关于使用它的文章:

http://blogs.msdn.com/b/suzcook/archive/2003/05/29/57120.aspx http://blogs.msdn.com/b/suzcook/archive/2003/05/29/57120.aspx

EDIT: 编辑:

Where are the DLL's stored? DLL存储在哪里? This forum post details a similar problem and the problem was down to the DLL's being referenced not being in either the GAC, executable directory or a sub-folder from the executable directory: 这篇论坛帖子详细介绍了一个类似的问题,问题在于引用的DLL不在GAC,可执行目录或可执行目录的子文件夹中:

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM