简体   繁体   English

在C#中未处理TypeLoadException

[英]TypeLoadException was unhandled in C#

I'm fairly new to C#, and am having a problem when loading a library into my program. 我对C#很新,在将程序库加载到我的程序时出现问题。 Im trying to run this example in visual studio, but I am getting an error: 我试图在visual studio中运行示例,但我收到一个错误:

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

This is what my code looks like: 这就是我的代码:

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);
    }
}

} }

I have added the dll as a reference via the solution explorer. 我已经通过解决方案资源管理器添加了dll作为参考。 What could be going wrong? 怎么可能出错?


I have started a new project, added the dll as a reference, ran the project and now everything works. 我已经开始了一个新项目,添加了dll作为参考,运行项目,现在一切正常。 Very frustrating not to know what went wrong, but I suspect it had something to do with the project name and the dll name being the same. 非常沮丧,不知道出了什么问题,但我怀疑它与项目名称有关,而dll名称是相同的。 Thanks for helping! 谢谢你的帮助!

EDIT: Okay, due to your answer, I've now managed to reproduce the problem without SVM. 编辑:好的,由于你的回答,我现在设法重现没有SVM的问题。 Basically, you shouldn't have two assemblies with the same name, one in a .exe and one in a .dll. 基本上,您不应该有两个具有相同名称的程序集,一个在.exe中,一个在.dll中。 Here's an example: 这是一个例子:

Library.cs: Library.cs:

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

Test.cs: test.cs中:

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

Compile: 编译:

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

Run: 跑:

> 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)

It's already loaded an assembly called Test from Test.exe... so it's not going to also look for Test.dll. 它已经加载了一个名为Test from Test.exe的程序集......因此它不会同时寻找Test.dll。

I wanted to add this as a comment (but not high enough rep yet) - I had this exact issue and found @JonSkeet answer really useful, between myself and a colleague we stumbled on the answer; 我想把它添加为评论(但还不够高的代表) - 我有这个确切的问题,发现@JonSkeet的答案非常有用,在我和我的同事之间偶然发现答案;

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

Basically my project assembly which generated an EXE file was named the same as a referenced assembly I built as a class library. 基本上我生成EXE文件的项目程序集的命名与我作为类库构建的引用程序集相同。 The combination of the EXE and DLL in the build directory cause the error to be thrown as only one assembly of that name could be loaded. 构建目录中的EXE和DLL的组合会导致抛出错误,因为只能加载该名称的一个程序集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM