2012-03-08 146 views
10

我對C#相當陌生,並且在將庫加載到我的程序時遇到問題。我試着在Visual Studio中運行this的例子,但我得到了一個錯誤:TypeLoadException在C#中未處理#

TypeLoadException was unhandled. Can't load type SVM.Problem from assembly SVM, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null. 

這是我的代碼如下所示:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using SVM; 

namespace SVM 
{ 
class Program 
{ 
    static void Main(string[] args) 
    { 
     //First, read in the training data. 
     Problem train = Problem.Read("a1a.train"); 
     Problem test = Problem.Read("a1a.test"); 

     //For this example (and indeed, many scenarios), the default 
     //parameters will suffice. 
     Parameter parameters = new Parameter(); 
     double C; 
     double Gamma; 

     //This will do a grid optimization to find the best parameters 
     //and store them in C and Gamma, outputting the entire 
     //search to params.txt. 
     ParameterSelection.Grid(train, parameters, "params.txt", out C, out Gamma); 
     parameters.C = C; 
     parameters.Gamma = Gamma; 

     //Train the model using the optimal parameters. 
     Model model = Training.Train(train, parameters); 

     //Perform classification on the test data, putting the 
     //results in results.txt. 
     Prediction.Predict(test, "results.txt", model, false); 
    } 
} 

}

我已經加入了DLL作爲通過解決方案瀏覽器進行參考。可能會出現什麼問題?


我已經開始一個新項目,添加了dll作爲參考,運行該項目,現在一切正常。非常沮喪不知道出了什麼問題,但我懷疑它與項目名稱和dll名稱是相同的。感謝您的幫助!

+0

需要了解詳情,哪些程序集是程序和問題英寸哪條線導致異常。你編譯的每個程序集是什麼平臺,是特定於引用版本的?你有沒有嘗試刪除bin和obj目錄並重建? – 2012-03-08 19:45:14

+0

你是什麼意思集會?沒有提到的行會導致錯誤,不幸的是 – Freek8 2012-03-08 19:57:13

+0

EXE和DLL被稱爲程序集。 – 2012-03-09 21:24:42

回答

23

編輯:好的,由於你的答案,我現在設法重現沒有SVM的問題。基本上,你不應該有兩個同名的程序集,一個在.exe中,一個在.dll中。這裏有一個例子:

Library.cs:

public class Library 
{ 
    public static void Foo() 
    { 
     System.Console.WriteLine("Library.Foo"); 
    } 
} 

test.cs中:

public class Test 
{ 
    static void Main(string[] args) 
    { 
     Library.Foo(); 
    } 
} 

編譯:

> csc /target:library /out:Test.dll Library.cs 
> csc /r:Test.dll Test.cs 

運行:

> test.exe 

Unhandled Exception: System.TypeLoadException: Could not load type 'Library' from 
assembly 'Test, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.+ 
    at Test.Main(String[] args) 

它已經從Test.exe加載了一個名爲Test的程序集......所以它不會去尋找Test.dll。

+0

謝謝,我可以在哪裏更改這個變量?如果我手動將dll複製到項目文件夾,會有幫助嗎? – Freek8 2012-03-08 19:43:58

+1

@ Freek8:如果您在突出顯示引用時查看「屬性」窗口,應該在那裏顯示引用。你是如何嘗試運行該程序的?你是如何添加參考的? – 2012-03-08 19:45:52

+0

我右鍵點擊引用>添加並選擇了dll。 CopyLocal已經是真的。我通過調試>啓動調試 – Freek8 2012-03-08 19:48:42

2

我想將此添加爲評論(但還不夠高) - 我有這個確切的問題,並發現@JonSkeet答案真的有用,在我和同事之間我們偶然發現了答案;

https://stackoverflow.com/a/13236893/692942

基本上我生成一個EXE文件的項目組件被命名爲與我構建爲一個類庫的引用程序集相同。構建目錄中的EXE和DLL的組合會導致錯誤被拋出,因爲只有該名稱的一個程序集可以加載。

相關問題