TypeInitializationException thrown for Program class

后端 未结 13 813
醉梦人生
醉梦人生 2020-12-10 23:30

My Windows Forms application was working earlier, however suddenly it stopped working. I am getting following exception:

相关标签:
13条回答
  • 2020-12-11 00:24

    Make sure you are not missing any dependency DLLs. In my case, a DLL I was referencing had a dependency on another DLL.

    If this is what happened, look at the "Inner Exception" property and then you will see a better error message. In my case, it said "Cannot find xxx.dll"

    0 讨论(0)
  • 2020-12-11 00:27

    After trying all of the answers listed here, I have a new one:

    The type initializer for 'MyLibrary' threw an exception.

    If you see something like the below in the InnerException(s)..........

    "Could not load file or assembly 'log4net, Version=1.2.13.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a' or one of its dependencies. The system cannot find the file specified.":"log4net, Version=1.2.13.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a"
    

    In my case, my directory had the correct version dll. (log4net.dll in this case).

    And then ... the issue was found. Assembly redirects in the app.config.

    :(

    Since I had the correct version, I removed all my redirects. Your situation may be different.

      <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
          <dependentAssembly>
            <assemblyIdentity name="log4net" publicKeyToken="b32731d11ce58905" />
            <codeBase version="1.2.9.0" href="log4netv1.2.9.0\log4net.dll" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="log4net" publicKeyToken="1b44e1d426115821" />
            <codeBase version="1.2.10.0" href="log4netv1.2.10.0\log4net.dll" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="log4net" publicKeyToken="669e0ddf0bb1aa2a" />
            <codeBase version="1.2.13.0" href="log4netv1.2.13.0\log4net.dll" />
          </dependentAssembly>
        </assemblyBinding>
      </runtime>
    
    0 讨论(0)
  • 2020-12-11 00:29

    This is one weird issue I had to deal with for the last 2 hours. I solved it by removing the static from the lists I created.

    private static readonly List<Person> someList = GlobalConfiguration.Connection.PopulateList();
    

    with this one:

    private readonly List<Person> someList = GlobalConfiguration.Connection.PopulateList();
    

    Hope it helps and you don't have to spend two hours to find out the bug...

    0 讨论(0)
  • 2020-12-11 00:30

    In my case the reason was, that <configSections> wasn't first one in config file.

    Only one <configSections> element allowed per config file and if present must be the first child of the root <configuration> element.

    Move the configSections element to the top in your config file.

    Hope it helps to someone.

    0 讨论(0)
  • 2020-12-11 00:31

    Another possible reason: the app.config has duplicate sections.

    0 讨论(0)
  • 2020-12-11 00:33

    So: either one of the field-initializers, or the static constructor, for Program - is failing. Find out why. Note: the InnerException has the actual exception that was raised, but basicaly: just debug the field initializers and static constructor. So look inside the Program class for either:

    static SomeType someField = /* some non-trivial expression or method call */ 
    

    or:

    static Program() {
        // stuff
    }
    
    0 讨论(0)
提交回复
热议问题