【问题标题】:TypeLoadException was unhandled in C#TypeLoadException 在 C# 中未处理
【发布时间】:2012-11-08 21:53:35
【问题描述】:

我是 C# 的新手,在将库加载到我的程序时遇到问题。我试图在 Visual Studio 中运行 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 名称相同有关。感谢您的帮助!

【问题讨论】:

  • 需要从细节,哪些程序集是程序和问题。哪一行导致异常。你们每个程序集是在什么平台上编译的,参考版本是特定的吗?您是否尝试删除 bin 和 obj 目录并重建?
  • 组装是什么意思?不幸的是,没有提到导致错误的行
  • EXE 和 DLL 被称为程序集。

标签: c# dll


【解决方案1】:

编辑:好的,由于您的回答,我现在已经设法在没有 SVM 的情况下重现该问题。基本上,您不应该有两个具有相同名称的程序集,一个在 .exe 中,一个在 .dll 中。这是一个例子:

库.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。

【讨论】:

  • 谢谢,我在哪里可以更改这个变量?如果我手动将dll复制到项目文件夹中会有帮助吗?
  • @Freek8:如果您在突出显示引用时查看“属性”窗口,它应该显示在那里。您是如何尝试运行该程序的?您是如何添加引用的?
  • @Freek8:如果您浏览到 bin/Debug 或 bin/Release 目录,您是否在其中看到了可执行文件和 DLL?如果你双击它,这有效吗?我试过你的代码,没有遇到这个问题...
  • @Freek8:听起来很奇怪,恐怕……不知道。
  • @Freek8:我有个主意...右键单击 DLL,然后打开“属性”框 - 有没有显示“解除阻止”的按钮?如果是这样,请单击该按钮,然后重试。
【解决方案2】:

我想将此作为评论添加(但代表还不够高)-我遇到了这个确切的问题并发现 @JonSkeet 的答案非常有用,在我自己和我们偶然发现答案的同事之间;

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

基本上,生成 EXE 文件的项目程序集的名称与我构建为类库的引用程序集相同。构建目录中的 EXE 和 DLL 的组合会导致引发错误,因为只能加载该名称的一个程序集。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多