"TypeInitializationException未处理"

Cod*_*key 9 c# xna visual-studio-2008

错误日志:

System.TypeInitializationException was unhandled
  Message="The type initializer for 'MaxDavidMP4.Program' threw an exception."
  Source="MaxDavidMP4"
  TypeName="MaxDavidMP4.Program"
  StackTrace:
       at MaxDavidMP4.Program.Main()
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.IO.FileNotFoundException
       Message="Could not load file or assembly 'Microsoft.Xna.Framework, Version=3.1.0.0, Culture=neutral, PublicKeyToken=6d5c3888ef60e27d' or one of its dependencies. The system cannot find the file specified."
       Source="MaxDavidMP4"
       FileName="Microsoft.Xna.Framework, Version=3.1.0.0, Culture=neutral, PublicKeyToken=6d5c3888ef60e27d"
       FusionLog="=== Pre-bind state information ===\r\nLOG: User = Max-PC\\Max\r\nLOG: DisplayName = Microsoft.Xna.Framework, Version=3.1.0.0, Culture=neutral, PublicKeyToken=6d5c3888ef60e27d\n (Fully-specified)\r\nLOG: Appbase = file:///C:/Users/Max/Desktop/maximas save/School/University/CSS 450/MaxDavidMP4/MaxDavidMP4/bin/Debug/\r\nLOG: Initial PrivatePath = NULL\r\nCalling assembly : UWBGL_XNA_Lib10, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.\r\n===\r\nLOG: This bind starts in default load context.\r\nLOG: No application configuration file found.\r\nLOG: Using machine configuration file from C:\\Windows\\Microsoft.NET\\Framework64\\v2.0.50727\\config\\machine.config.\r\nLOG: Post-policy reference: Microsoft.Xna.Framework, Version=3.1.0.0, Culture=neutral, PublicKeyToken=6d5c3888ef60e27d\r\nLOG: The same bind was seen before, and was failed with hr = 0x80070002.\r\n"
       StackTrace:
            at MaxDavidMP4.Model..ctor()
            at MaxDavidMP4.Program..cctor() in C:\Users\Max\Desktop\maximas save\School\University\CSS 450\MaxDavidMP4\MaxDavidMP4\Program.cs:line 14
       InnerException: 
Run Code Online (Sandbox Code Playgroud)

Program.cs代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace MaxDavidMP4
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        /// 
        static Model model = new Model();

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }

        public static Model getModel()
        {
            return model;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

似乎问题是Microsoft.Xna.Framework 3.1.0包含,但我确定路径是正确的,并且我的所有其他项目都可以正常使用此设置.这是在VS2008,C#,顺便说一句.我记得上次遇到这个问题时,我不得不将VS2008中的一个顶级中央下拉列表设置为'x86'并修复它,但现在它没有任何区别.

Emp*_*ean 10

您可能遇到过上次遇到的同样问题.我想不出你会得到那个例外的另一个原因.您可能没有正确实施此修复程序.

请注意,这是内部异常.如果您在程序的主体中创建了Model,而不是在静态初始化期间,则外部异常将消失.

所以你的例外基本上是:

无法加载程序集"Microsoft.Xna.Framework".该系统找不到指定的文件.

肖恩哈格里夫斯写道,这个例外真的意味着:

无法将32位程序集"Microsoft.Xna.Framework"加载到64位进程中.当你的游戏项目应该指定'x86'时,你的游戏项目被设置为'Any CPU'平台.

修复是:

在Visual Studio工具栏中,应该有一个组合框,上面写着"任何CPU".

如果您使用的是C#Express,则此工具栏条目可能会显示为灰色.要启用它,请转到"工具/选项",选中"显示所有设置",选择"项目和解决方案"选项卡,然后选中"显示高级构建配置"框.

下拉"Any CPU"工具栏组合,然后选择"Configuration Manager".打开"Active solution platform"组合,选择" <New...>",然后创建"x86"配置.


Joh*_*ack 6

我的程序Main()的第一行出现了同样的错误,它已经完美地工作了好几天.它与加载程序集无关.问题是我在同一个实用程序类的声明部分中调用了静态实用程序类的方法时不小心粘贴了.例:

public class Utility
{
    public static int i = Utility.SomeStaticMethod();

    public static int SomeStaticMethod()
    {
        return 1;
    }
}
Run Code Online (Sandbox Code Playgroud)

我并不完全理解上面发生的内部内容,但我知道这是问题,因为只要我将该调用移动到Init()方法中,TypeInitializationException就会消失.我的猜测是,虽然上述内容在编译器POV中是合法的,但在运行时它遇到了尚未定义的方法的使用.由于这被用作一个完全静态的类,因此该类不会在与其他所有内容相同的范围内"初始化",而是以某种方式在正常异常流不可用的场景之后进行初始化.

思考?