2012-03-08 58 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的组合会导致错误被抛出,因为只有该名称的一个程序集可以加载。