我对C#来说相当新鲜,在将程序库加载到程序中时遇到问题.我试图在视觉工作室运行
this的例子,但我收到一个错误:
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名称是一样的.感谢您的帮助!
解决方法
编辑:好的,由于你的答案,我现在已经设法重现了没有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,PublicKeyToken=null'.+ at Test.Main(String[] args)
它已经加载了一个名为Test from Test.exe的程序集,所以它不会去寻找Test.dll.