12

I'm fairly new to C#, and am having a problem when loading a library into my program. Im trying to run this example in visual studio, but I am getting an error:

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. What could be going wrong?


I have started a new project, added the dll as a reference, ran the project and now everything works. 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. Thanks for helping!

3
  • Need from details, which assemblies are Program and Problem in. Which line causes the exception. What platform are each of you assemblies compiled in, are the references version specific? Did you try to delete the bin and obj directories and rebuild? Mar 8, 2012 at 19:45
  • What do you mean with assembly? There is no line mentioned that causes the error unfortunately
    – Freek8
    Mar 8, 2012 at 19:57
  • EXE and DLLs are called assemblies. Mar 9, 2012 at 21:24

2 Answers 2

25

EDIT: Okay, due to your answer, I've now managed to reproduce the problem without SVM. Basically, you shouldn't have two assemblies with the same name, one in a .exe and one in a .dll. Here's an example:

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

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.

14
  • thanks, where can I changed this variable? Would it help if I manually copied the dll to the project folder?
    – Freek8
    Mar 8, 2012 at 19:43
  • 1
    @Freek8: If you look at the "Properties" window when you've got the reference highlighted, it should be shown there. How did you try to run the program? How did you add the reference?
    – Jon Skeet
    Mar 8, 2012 at 19:45
  • 1
    @Freek8: If you browse to the bin/Debug or bin/Release directory, do you see both your executable and the DLL there? If you double-click on it, does that work? I've tried your code, and didn't get that problem...
    – Jon Skeet
    Mar 8, 2012 at 20:21
  • 1
    @Freek8: Sounds very odd, I'm afraid... no idea.
    – Jon Skeet
    Mar 8, 2012 at 20:42
  • 1
    @Freek8: I've had an idea... right-click on the DLL, and open up the "Properties" box - is there a button saying "Unblock"? If so, click that and then try again.
    – Jon Skeet
    Mar 9, 2012 at 8:19
3

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;

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

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.