系统突然发生故障.TypeInitializationException, Visual Studio不会指出任何错误

本文关键字:错误 任何 Studio Visual 突然 故障 TypeInitializationException 系统 | 更新日期: 2023-09-27 18:16:37

在处理了一个表单应用程序一整天之后,我创建了一个简单的UI和一些按键事件。太棒了!但是现在,我去运行它,它突然开始抛出system。typeinitializationexception。通常情况下,我可以自己找出这样的问题(在Visual Studio的帮助下),但是Visual Studio并没有发现我编码的任何错误。

这是我的表单:

    public Form1()
    {
        Shown += new EventHandler(FormShow);
        KeyDown += new KeyEventHandler(Form1_KeyDown);
        InitializeComponent();
    }
    // ------------------------------- //
    // --------- Game Code ----------- //
    // ------------------------------- //
    public void FormShow(object sender, System.EventArgs e)
    {
        GameState = 0;
        //Begin HUDRendererThread
        StartHUDRenderThread(this);
        GameState = 2;
    }
    // ------------------------------- //
    // -- Methods Used By This File -- //
    // ------------------------------- //
    public void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.H)
        {
            DamagePlayer(ThePlayer, 5);
        }
    }
    public void DamagePlayer(Player playa, int damage)
    {
        playa.playerHP = playa.playerHP - damage;
    }
    // ------------------------------- //
    // ----- HUD Renderer Thread ----- //
    // ------------------------------- //
    public class ThreadForRenderingHUD
    {
        public Form from;
        public GameHUD initRenderHUD(Player playerUsed, string desiredName)
        {
            GameHUD newHUD = new GameHUD();
            newHUD.Name = desiredName;
            newHUD.playerToGetStatisticsFrom = playerUsed;
            return newHUD;
        }
        public void GetSettedHP(GameHUD gamehud)
        {
            if (gamehud.playerToGetStatisticsFrom.playerHP >= (gamehud.playerToGetStatisticsFrom.playerHPMaxValue * 0.8))
            {
                gamehud.HP = 5;
            }
            if (gamehud.playerToGetStatisticsFrom.playerHP >= (gamehud.playerToGetStatisticsFrom.playerHPMaxValue * 0.6) & gamehud.playerToGetStatisticsFrom.playerHP < (gamehud.playerToGetStatisticsFrom.playerHPMaxValue * 0.8))
            {
                gamehud.HP = 4;
            }
            if (gamehud.playerToGetStatisticsFrom.playerHP >= (gamehud.playerToGetStatisticsFrom.playerHPMaxValue * 0.4) & gamehud.playerToGetStatisticsFrom.playerHP < (gamehud.playerToGetStatisticsFrom.playerHPMaxValue * 0.6))
            {
                gamehud.HP = 3;
            }
            if (gamehud.playerToGetStatisticsFrom.playerHP >= (gamehud.playerToGetStatisticsFrom.playerHPMaxValue * 0.2) & gamehud.playerToGetStatisticsFrom.playerHP < (gamehud.playerToGetStatisticsFrom.playerHPMaxValue * 0.4))
            {
                gamehud.HP = 2;
            }
            if (gamehud.playerToGetStatisticsFrom.playerHP >= (gamehud.playerToGetStatisticsFrom.playerHPMaxValue * 0) & gamehud.playerToGetStatisticsFrom.playerHP < (gamehud.playerToGetStatisticsFrom.playerHPMaxValue * 0.2))
            {
                gamehud.HP = 1;
            }
            if (gamehud.playerToGetStatisticsFrom.playerHP <= (gamehud.playerToGetStatisticsFrom.playerHPMaxValue * 0))
            {
                gamehud.HP = 0;
            }
        }
        public void RenderHUD()
        {
            Player guy = ThePlayer;
            GameHUD ThisGamesHUD = initRenderHUD(guy, "PrimaryGameHUD");
            guy.playerHP = 100;
            guy.playerHPMaxValue = 100;
            guy.playerName = "The Hero!";
            string HPRepresenterHealthBoxIndex = @"C:'Users'Kent Brown'Documents'Visual Studio 2012'Projects'GameTesting'images'HUD'HealthRenderer'";
            PictureBox HPRepresenter = new PictureBox();
            HPRepresenter.AutoSize = true;
            Debug.Print(from.Name);
            from.Invoke(new Action(delegate() { from.Controls.Add(HPRepresenter); }));
            string wait = "nil";
            Debug.Print("GAMESTATE HAS BEEN 2");
            while (GameState == 2)
            {
                // --- HP Renderer --- //
                GetSettedHP(ThisGamesHUD);
                wait = HPRepresenterHealthBoxIndex + Convert.ToString(ThisGamesHUD.HP) + ".png";
                from.Invoke(new Action(delegate() { HPRepresenter.Load(wait); }));
                if (guy.playerHP <= (guy.playerHPMaxValue * 0))
                {
                    Label newLabel = new Label();
                    newLabel.Text = "YOU HAVE DIED...";
                    newLabel.Font = new Font(DeathFontFamily, 24, FontStyle.Regular);
                    from.Invoke(new Action(delegate() { from.Controls.Add(newLabel); }));
                }
            }
        }
    };
    public void StartHUDRenderThread(Form frm)
    {
        ThreadForRenderingHUD HUDRenderer = new ThreadForRenderingHUD();
        HUDRenderer.from = frm;
        Thread RenderThread = new Thread(new ThreadStart(HUDRenderer.RenderHUD));
        RenderThread.Start();
        while (!RenderThread.IsAlive) ;
    }
    // ------------------------------- //
    // -- Classes(Players/HUD, etc) -- //
    // ------------------------------- //
    public class GameHUD
    {
        public int HP;
        public string Name;
        public Player playerToGetStatisticsFrom;
    }
    public class Player
    {
        public int playerHP;
        public int playerHPMaxValue;
        public string playerName;
    }
    public static FontFamily DeathFontFamily = new FontFamily("Chiller");
    // ------------------------------- //
    // ------ Stuff Declaration ------ //
    // ------------------------------- //
    public static Player ThePlayer = new Player();
    public static int GameState;
    // GameState value references: 0) Initializing 1) Initialized 2) Playing //'

当Visual Studio抛出这个异常时,它突出显示了Program.cs中的这三行:

        Application.Run(new Form1());

它给出了异常,Visual Studio不应该找出原因吗?如果有人能找到问题的根源,你能指出来就太好了。谢谢!

系统突然发生故障.TypeInitializationException, Visual Studio不会指出任何错误

注意static成员初始化