C# System.TypeInitializationException突然发生,Visual Studio未指出任何错误
在使用表单应用程序一整天之后,我创建了一个简单的UI和一些按键事件。伟大的但现在,我去运行它,它突然开始抛出System.TypeInitializationException。通常情况下,我可以自己(在VisualStudio的帮助下)解决这样的问题,但是VisualStudio没有发现我的编码有任何错误 这是我的Form1.cs:C# System.TypeInitializationException突然发生,Visual Studio未指出任何错误,c#,exception,C#,Exception,在使用表单应用程序一整天之后,我创建了一个简单的UI和一些按键事件。伟大的但现在,我去运行它,它突然开始抛出System.TypeInitializationException。通常情况下,我可以自己(在VisualStudio的帮助下)解决这样的问题,但是VisualStudio没有发现我的编码有任何错误 这是我的Form1.cs: public Form1() { Shown += new EventHandler(FormShow); Key
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 //'
它给出了一个例外,VisualStudio不应该找出原因吗?如果有人能找到问题的根源,如果你能指出,那就太好了。谢谢 注意
静态
成员初始化。考虑到您得到的是一个特定错误,您是否也能够获得堆栈跟踪?当您说,它是“给出异常”时,它只是突出显示应用程序。运行
行并显示异常对话框窗口调出内容?是的,这是它唯一做的事情。遗憾的是,我没有从中获取任何类型的错误日志的选项。在该对话框中,视图中应该有一部分称为“堆栈跟踪”。如果您可以复制该值的内容,这可能会有所帮助。我没有看到任何内容。当我得到异常窗口时,唯一的选项是“中断”和“继续”。
Application.Run(new Form1());