TypeLoadException was unhandled in C#

前端 未结 2 954
广开言路
广开言路 2021-01-04 08:44

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:

<         


        
2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-04 08:46

    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.

提交回复
热议问题