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

弗兰克·哈登伯格

所以我在一个学校项目中工作,我将文本转换为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。

法比安

通常System.TypeInitializationException意味着某些其他任何操作之前初始化的静态成员都会引发异常。

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

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

sed为什么会出现国际字符失败以及如何解决?

sed为什么会出现国际字符失败以及如何解决?

什么是错误?以及如何解决

SQL Server:出现错误“ Msg147,level15”为什么会出现此错误以及如何解决此错误

尝试检查碰撞时为什么会出现类型错误,我该如何解决?

为什么会出现“多个定义”错误?我如何解决它?

为什么会出现“应用程序不带参数”的提示,我该如何解决?

为什么会出现此错误,我该如何解决?迅速

为什么它不旋转呢?以及如何解决?

JavaFX为什么会模糊控件,以及如何解决?

phpmyadmin受阻,为什么以及如何解决它?

无法注册硬盘VBox。为什么以及如何解决?

为什么pygame滞后以及如何解决?

为什么我在Spring Boot上出现TLSv1问题以及如何解决?

为什么将图片居中时出现1px舍入错误,以及如何解决?

戴尔 G5 15 5500 会出现哪些问题以及如何解决这些问题?

Android:为什么会出现这些错误,以及解决这些错误的简便方法是什么?

到底什么是不可迭代的,以及如何解决?

Node.js:什么是ENOSPC错误以及如何解决?

MissingManifestResourceException是什么意思,以及如何解决它?

为什么会出现“错误:解决方法指定过多”?

为什么会出现“无法解决的外部”错误?

为什么会出现错误“ JSON对象必须为str,而不是'bytes'”,并且该如何解决?

为什么 Node.js 上会出现“TypeError: client.db is not a function”,我该如何解决?

我的脚本出现语法错误,但我不知道为什么以及如何解决它>

为什么会出现错误“Segmentation fault 11”以及如何在 C 中修复它?

为什么Tk()。after仅执行一次以及如何解决?

Shell脚本使用bash运行,但不使用sh运行。为什么以及如何解决?

为什么ansi转义不适用于代码块,以及如何解决?