为什么会出现System.TypeInitializationException以及如何解决?

时间:2018-10-11 09:34:24

标签: c#

因此,我在一个学校项目中工作,我将文本转换为morsecode,反之亦然。 当我仍在整理布局时,一切工作正常,每当我单击以第一种形式创建的按钮时,它们都会重定向到另一种形式并关闭原始形式。现在,我添加了一个词典和一些变量,突然我收到了这个错误。 我已经尝试过谷歌搜索,但似乎找不到适合我的解决方案。 希望有人可以在这里帮助我吗?

//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。

1 个答案:

答案 0 :(得分:4)

通常,System.TypeInitializationException意味着某些在之前初始化的静态成员会抛出异常。

在您的情况下,静态字典ToMorse在此处具有重复的add语句:

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

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