簡體   English   中英

Visual Studio在哪里查找程序集?

[英]Where does Visual Studio look for assemblies?

我有一個框架,其中包含許多基類,可以派生它們來開發許多應用程序。 在這些類中有一個System.Windows.Forms.Panel的子類,我為它編寫了自己的設計器。 Visual Studio 2005一切正常,但當我嘗試遷移到VS2010時出現問題。 這是我正在做的簡化版本:

我有一個名為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;
    }
}

然后有一個項目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";
    }
}

最后有一個控件項目,它只引用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();
    }
}

現在我創建另一個引用所有這些dll的解決方案,並包含一個SimplePanel的空子類。 當我在SolutionExplorer中雙擊此類時,將執行SimplePanelDesigner的構造函數並調用ConfLoader的方法LoadConf。 這意味着ConfClasses.dll以dinamically方式加載,並創建一個ConfClass實例。 到目前為止一切都很好,但是當調用InitFoo時會引發異常:

無法加載文件或程序集“CoreClasses,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null”或其依賴項之一。 該系統找不到指定的文件。

為了使事情變得更難,在這個例子中實際上沒有提出異常,但這正是我真正的應用程序正在執行的那種結構,以及我得到的異常。 我還沒有弄清楚這里發生了什么。 VS正在執行CoreClasses中的IS方法。 為什么要再次加載它? 它在哪里尋找它? 我也檢查了當前的AppDomain,但它在其加載的程序集中有CoreClasses,它似乎沒有改變。

只是為了添加更多細節,每個項目都構建在一個公共文件夾中(不是項目文件夾中常用的obj / debug文件夾),而且在我開始測試時,PC上沒有其他的dll副本。 然后所有引用的dll的副本在我的userprofile的AppData \\ Local \\ Microsoft \\ VisualStudio \\ 10.0 \\ ProjectAssemblies文件夾中的一系列文件夾中完成,這似乎是VS在Assembly.Load中查找程序集的地方執行,我可以在那里找到CoreClasses的副本。 我試圖清理所有文件夾,重建所有內容,並在每個組合中保持打開/關閉不同的解決方案,但沒有任何改進。

編輯:

正如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).

編輯2

只是為了添加一些信息,我看了一下我的簡單例子生成的融合日志,發現在嘗試加載CoreClasses時生成了完全相同的日志,但是VisualStudio找到了解決它的方法。

我剛剛發現了正在發生的事情。 簡單示例和真實事物之間的區別在於涉及AddIn。 這是一個相當日志的故事,但就是這樣。
從代碼示例中可以看出,我通過反射加載ConfClasses dll,以避免添加對它的引用。 這在運行時很好,但設計師抱怨說它無法將IConf轉換為IConf。 發生這種情況是因為CoreClasses.dll是在設計器啟動時從AppData \\ Local \\ Microsoft \\ VisualStudio \\ 10.0 \\ ProjectAssemblies加載的,但ConfClasses.dll是從我的bin文件夾加載的,因此它的引用也是如此,因此有兩個版本的CoreClasses.dll和不同版本的IConf。
為了繞過這個問題,我開發了一個AddIn,當在設計時使用Assembly.Load加載程序集時,添加對該程序集的引用,然后在關閉最后一個設計器窗口時清除引用。
VS2005一切正常,但使用ProcMon.exe我發現VS2010添加了一個新文件夾,其中插件尋找程序集: Program Files(x86)\\ Microsoft Visual Studio 10.0 \\ Common7 \\ IDE \\ CommonExtensions \\ DataDesign
我在那里復制了我的裝配,一切都恢復了。 現在只需要找到一種手動添加東西的方法。

您是否嘗試使用程序集綁定日志查看器來調查加載程序集失敗的原因?

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

這里有另一篇關於使用它的文章:

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

編輯:

DLL存儲在哪里? 這篇論壇帖子詳細介紹了一個類似的問題,問題在於引用的DLL不在GAC,可執行目錄或可執行目錄的子文件夾中:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM