c# - 为什么我会收到 System.TypeInitializationException 以及如何修复它?
问题描述
所以我正在做一个学校项目,我将文本转换为莫尔斯电码,反之亦然。当我还在制作布局时,一切正常,每当我点击我在第一个表单中制作的一个按钮时,它们就会重定向到另一个表单并关闭原始表单。现在我添加了一个字典和一些变量,突然间我得到了这个错误。我已经尝试过谷歌搜索,但似乎找不到适合我的解决方案。希望有人可以在这里帮助我吗?
//Main Form
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MorseCode
{
public partial class Morsecode : Form
{
public Morsecode()
{
InitializeComponent();
}
private void ConvertToMorse_Click(object sender, EventArgs e)
{
this.Hide();
ConvertToMorse Morse = new ConvertToMorse();
Morse.ShowDialog();
}
private void ConvertToText_Click(object sender, EventArgs e)
{
this.Hide();
ConvertToText Text = new ConvertToText();
Text.ShowDialog();
}
private void Morsecode_FormClosing(object sender, FormClosingEventArgs e)
{
Application.Exit();
}
private void ConvertToMorse_FormClosing(object sender, FormClosingEventArgs e)
{
Application.Exit();
}
private void ConvertToText_FormClosing(object sender, FormClosingEventArgs e)
{
Application.Exit();
}
}
}
//Second Form
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MorseCode
{
public partial class ConvertToMorse : Form
{
string InputString = "";
List<char> TextInput;
static Dictionary<char, string> ToMorse = new Dictionary<char, string>()
{
{'A', ". _"},
{'B', "_ . . ."},
{'C', "_ . _ ."},
{'D', "_ . ."},
{'E', "."},
{'F', ". . _ ."},
{'G', "_ _ ."},
{'H', ". . . ."},
{'I', ". ."},
{'J', ". _ _ _"},
{'K', "_ . _"},
{'L', ". _ . ."},
{'M', "_ _"},
{'N', "_ ."},
{'O', "_ _ _"},
{'P', ". _ _ ."},
{'Q', "_ _ . _"},
{'R', ". _ ."},
{'S', ". . ."},
{'T', "_"},
{'U', ". . _"},
{'V', ". . . _"},
{'W', ". _ _"},
{'X', "_ . . _"},
{'Y', "_ . _ _"},
{'Z', "_ _ . ."},
{'0', "_ _ _ _ _"},
{'1', ". _ _ _ _"},
{'2', ". . _ _ _"},
{'3', ". . . _ _"},
{'4', ". . . . _"},
{'5', ". . . . ."},
{'6', "_ . . . ."},
{'7', "_ _ . . ."},
{'8', "_ _ _ . ."},
{'9', "_ _ _ _ ."},
{'.', ". _ . _ . _"},
{',', "_ _ . . _ _"},
{'?', ". . _ _ . ."},
{'!', "_ . _ . _ _"},
{'-', "_ . . . . _"},
{'/', "_ . . _ ."},
{':', "_ _ _ . . ."},
{'\'', ". _ _ _ _ ."},
{'-', "_ . . . . _"},
{'}', "_ . _ _ . _"},
{';', "_ . _ . _"},
{'{', "_ . _ _ ."},
{'=', "_ . . . _"},
{'@', ". _ _ . _ ."},
{'&', ". _ . . ."}
};
public ConvertToMorse()
{
InitializeComponent();
}
private void Input_TextChanged(object sender, EventArgs e)
{
}
private void ConvertText_Click(object sender, EventArgs e)
{
InputString = Input.Text;
foreach(char Text in InputString)
{
TextInput.Add(Text);
}
}
private void Output_TextChanged(object sender, EventArgs e)
{
}
}
}
//Third Form
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MorseCode
{
public partial class ConvertToText : Form
{
static Dictionary<char, string> ToMorse = new Dictionary<char, string>()
{
{'A', ". _"},
{'B', "_ . . ."},
{'C', "_ . _ ."},
{'D', "_ . ."},
{'E', "."},
{'F', ". . _ ."},
{'G', "_ _ ."},
{'H', ". . . ."},
{'I', ". ."},
{'J', ". _ _ _"},
{'K', "_ . _"},
{'L', ". _ . ."},
{'M', "_ _"},
{'N', "_ ."},
{'O', "_ _ _"},
{'P', ". _ _ ."},
{'Q', "_ _ . _"},
{'R', ". _ ."},
{'S', ". . ."},
{'T', "_"},
{'U', ". . _"},
{'V', ". . . _"},
{'W', ". _ _"},
{'X', "_ . . _"},
{'Y', "_ . _ _"},
{'Z', "_ _ . ."},
{'0', "_ _ _ _ _"},
{'1', ". _ _ _ _"},
{'2', ". . _ _ _"},
{'3', ". . . _ _"},
{'4', ". . . . _"},
{'5', ". . . . ."},
{'6', "_ . . . ."},
{'7', "_ _ . . ."},
{'8', "_ _ _ . ."},
{'9', "_ _ _ _ ."},
{'.', ". _ . _ . _"},
{',', "_ _ . . _ _"},
{'?', ". . _ _ . ."},
{'!', "_ . _ . _ _"},
{'-', "_ . . . . _"},
{'/', "_ . . _ ."},
{':', "_ _ _ . . ."},
{'\'', ". _ _ _ _ ."},
{'-', "_ . . . . _"},
{'}', "_ . _ _ . _"},
{';', "_ . _ . _"},
{'{', "_ . _ _ ."},
{'=', "_ . . . _"},
{'@', ". _ _ . _ ."},
{'&', ". _ . . ."}
};
Dictionary<string, Char> text = ToMorse.ToDictionary(e => e.Value, e => e.Key);
public ConvertToText()
{
InitializeComponent();
}
private void Input_TextChanged(object sender, EventArgs e)
{
}
private void ConvertText_Click(object sender, EventArgs e)
{
}
private void Output_TextChanged(object sender, EventArgs e)
{
}
}
}
我的 IDE 是 Visual Studio 2017。
解决方案
通常System.TypeInitializationException
意味着一些在其他任何事情之前初始化的静态成员抛出异常。
在您的情况下,静态字典ToMorse
在这里有一个重复的 add 语句:
...
{'-', "_ . . . . _"},
{'/', "_ . . _ ."},
{':', "_ _ _ . . ."},
{'\'', ". _ _ _ _ ."},
{'-', "_ . . . . _"}, // bang! duplicate key
...
当我们尝试两次添加相同的“-”键时,字典会抛出异常。确保您的所有 Dictionary 键都是唯一的并调试静态成员初始化以查看没有错误
推荐阅读
- python - 管理 conda 和 pip 之间依赖关系的最佳实践
- javascript - 组件仅在通过链接(单击)到达时正确呈现,而不是通过直接 URL/刷新
- google-calendar-api - 有人可以将我的应用程序中的事件添加到我的 Google 日历吗?(例如,类似于 Calendly?)
- javascript - Slickgrid:在单元格中输入的值在移出单元格后消失
- elasticsearch - 在 Grafana 仪表板上计算百分比
- vim - vim:替换包含特殊字符和模式跨越多行的模式
- javascript - Formik 重置回调创建递归问题
- multicast - ss 和 netstat 显示不同的多播组
- c# - 如何在 Windows 窗体 c#、sql 中根据他们的电子邮件地址保存不同的用户设置?
- c# - C# 使用 Listbox1 中的值进行 SQL 查询来过滤 Listbox2