在C#中未处理TypeLoadException
我对C#很新,在将程序库加载到程序时出现问题。 我试图在visual studio中运行这个例子,但我收到一个错误:
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, Culture=neutral, PublicKeyToken=null'.+ at Test.Main(String[] args)
它已经加载了一个名为Test
from Test.exe的程序集……因此它也不会查找Test.dll。
我想把它添加为评论(但还不够高的代表) – 我有这个确切的问题,发现@JonSkeet的答案非常有用,在我和我的同事之间偶然发现了答案;
https://stackoverflow.com/a/13236893/692942 。
基本上我生成EXE文件的项目程序集的命名与我作为类库构建的引用程序集相同。 构建目录中的EXE和DLL的组合会导致抛出错误,因为只能加载该名称的一个程序集。
- MaxDegreeOfParallelism = Environment.ProcessorCount减慢了CPU的执行时间
- 方法返回一个接口
- Git不能使用utf-16编码进行diff或merge .cs文件
- 正确使用DialogResult
- 要求对OWIN应用程序的所有请求进行身份validation
- 如何通过按“空格”取消Visual Studio 2015中的自动完成?
- DELETE语句与具有Entity Framework的SAME TABLE REFERENCE约束冲突
- 在Unity中使用dynamic keyword / .NET 4.6function
- 如何使用linq2sql和OR运算符使用谓词构建器