首页 > 解决方案 > 为什么我会收到 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。

标签: c#

解决方案


通常System.TypeInitializationException意味着一些在其他任何事情之前初始化的静态成员抛出异常。

在您的情况下,静态字典ToMorse在这里有一个重复的 add 语句:

...
{'-', "_ . . . . _"},
{'/', "_ . . _ ."},
{':', "_ _ _ . . ."},
{'\'', ". _ _ _ _ ."},
{'-', "_ . . . . _"},  // bang! duplicate key
...

当我们尝试两次添加相同的“-”键时,字典会抛出异常。确保您的所有 Dictionary 键都是唯一的并调试静态成员初始化以查看没有错误


推荐阅读