静态类:'***'的类型初始值设定项引发了异常。 [英] Static Classes: The type initializer for ' *** ' threw an exception.

查看:82
本文介绍了静态类:'***'的类型初始值设定项引发了异常。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个静态类和许多其他类。

静态类有很多变量,其中一个变量是双倍的:

  public   static   double  speed =  2 ; 



我尝试在程序开始时或稍微更改一下它的值:

 Data.speed =  2 ; 



和它抛出下一个异常:

Quote:

游戏XO中发生未处理的System.TypeInitializationException类型异常。 exe



附加信息:'Game_X_O.Data'的类型初始化程序引发异常。





但是当我在其他地方更改其值时,它工作正常。

如果我创建一个新的Windows窗体应用程序它也可以正常工作,这就是为什么我无法重现问题。



我还可以录制我的Visual Studio视频,以便在需要时显示问题。

如果我可以分享一些信息来帮助我,让我知道。



静态类:

<前lang =c#> 使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.Drawing;

命名空间 Game_X_O
{
静态 class 数据
{
public static double speed = 2 ;

静态 float plus = 1 ;
static int percent = 20 ;
static bool _in = true ;

public static 颜色item0_color = Color.White;
public static float item0_plus =加;
public static bool item0_in = _in;
public static int item0_percent =百分比;

public static 颜色item1_color = Color.Yellow;
public static float item1_plus =加;
public static bool item1_in = _in;
public static int item1_percent =百分比;

public static 颜色item2_color = Color.Red;
public static float item2_plus =加;
public static bool item2_in = _in;
public static int item2_percent =百分比;

public static Color lines_color = Color.White;
public static int lines_percent =百分比;
public static double lines_speed = 2D;

public static Color line_color = Color.White;
public static int line_percent =百分比;
public static int line_plus = 20 ;

public static int i1_size = 0 ;
public static int i2_size = Calculate.arr_size * Calculate.arr_size + 3 ;
public static int i3_size = i2_size + Calculate.arr_size * Calculate.arr_size + 1 ;
public static int i4_size = i3_size + Form1.form1.items.itemslength;

public static int i1_start = 0 ;
public static int i1_stop = i1_start;

public static int i2_start = 3 + Calculate.arr_size * Calculate.arr_size;
public static int i2_stop = i2_start;

public static int i3_start = 3 + Calculate.arr_size * Calculate.arr_size * 2 ;
public static int i3_stop = i3_start;

public static int i4_start;
public static int i4_stop ;
}
}





Form1 Class我称之为:

 使用系统; 
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Linq;
使用 System.Text;
使用 System.Threading;
使用 System.Windows.Forms;

命名空间 Game_X_O
{
public partial class Form1:Form
{
公开 Form1()
{
Data.speed = 5 ; // exception

InitializeComponent();

FormBorderStyle = FormBorderStyle.None;
this .WindowState = FormWindowState.Maximized;

this .DoubleBuffered = true ;

form1 = ;

w = System.Windows.Forms.Screen.GetBounds( this )。宽度;
h = System.Windows.Forms.Screen.GetBounds( this )。高度;

New();
}

public static Form1 form1;

public static int w;
public static int h ;
public static int 大小= 3 + Calculate.arr_size * Calculate.arr_size * 2 + New_Items.max_itemslength + 1 ;

public void 新()
{
di = new Draw_Items(form1);
game = new 游戏(form1);
rects = new 矩形(form1);
items = new New_Items(form1);
calc = new 计算(form1);
ls = new 行(form1);
ai = new Add_Items(form1);
draw = new Draw(form1);
ll = new Light_Lines(form1);
mga = new Move_Game_Area(form1);

Start();

ci = new Control_Items(form1);
}

public 游戏;
public 矩形rects;
public New_Items项目;
public 计算calc;
public 行ls;
public bool single = false ;
public Draw_Items di;
public Add_Items ai;
public 绘制抽奖;
public Light_Lines ll;
public Move_Game_Area mga;
public Control_Items ci;

private void Start()
{
game.Play();
}
}
}





InnerException:

Quote:

System.NullReferenceException:对象引用未设置为对象的实例。

在Game_X_O.Data..cctor ()in c:\ Users \ Ziya1995 \Desktop\Game XO 18 - Copy\Game XO\Data.cs:第42行

解决方案

好的,让我们来看看。静电初始化器故障是体袋材料。然后,该类型对于该过程的生命是无用的。它是由静态构造函数引发异常引起的。如果您将调试器设置为在第一次机会异常时中断,则会显示该行。



我的赌注就是这样:

 public static int i4_size = i3_size + Form1.form1.items.itemslength; 



内联中的内容在构造函数中隐式设置。静态构造函数在代码执行中第一次被提及时调用。在这里,你有一件事,生命周期不明确引用了另一件生命周期不明确的事情。



你能不能使用普通实例,这样你就可以确定它们在实例化了正确的订单?


I have a static class and lots of other classes.
Static Class has lots of variables and one of them is double:

public static double speed = 2;


I try to change its value at the start of the program or at a little further:

Data.speed = 2;


and it throws the next exception:

Quote:

An unhandled exception of type 'System.TypeInitializationException' occurred in Game X-O.exe

Additional information: The type initializer for 'Game_X_O.Data' threw an exception.



But when i change its value somewhere else, it works fine.
It also works fine if i create a new Windows Forms application and that is why i can't reproduce the problem.

I can also record a video of my Visual Studio to show the problem if needed.
If i can share some information to help, let me know.

Static Class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace Game_X_O
{
    static class Data
    {
        public static double speed = 2;

        static float plus = 1;
        static int percent = 20;
        static bool _in = true;

        public static Color item0_color = Color.White;
        public static float item0_plus = plus;
        public static bool item0_in = _in;
        public static int item0_percent = percent;

        public static Color item1_color = Color.Yellow;
        public static float item1_plus = plus;
        public static bool item1_in = _in;
        public static int item1_percent = percent;

        public static Color item2_color = Color.Red;
        public static float item2_plus = plus;
        public static bool item2_in = _in;
        public static int item2_percent = percent;

        public static Color lines_color = Color.White;
        public static int lines_percent = percent;
        public static double lines_speed = 2D;

        public static Color line_color = Color.White;
        public static int line_percent = percent;
        public static int line_plus = 20;

        public static int i1_size = 0;
        public static int i2_size = Calculate.arr_size * Calculate.arr_size + 3;
        public static int i3_size = i2_size + Calculate.arr_size * Calculate.arr_size + 1;
        public static int i4_size = i3_size + Form1.form1.items.itemslength;

        public static int i1_start = 0;
        public static int i1_stop = i1_start;

        public static int i2_start = 3 + Calculate.arr_size * Calculate.arr_size;
        public static int i2_stop = i2_start;

        public static int i3_start = 3 + Calculate.arr_size * Calculate.arr_size * 2;
        public static int i3_stop = i3_start;

        public static int i4_start;
        public static int i4_stop;
    }
}



Form1 Class where i call it:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Game_X_O
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            Data.speed = 5; // exception

            InitializeComponent();

            FormBorderStyle = FormBorderStyle.None;
            this.WindowState = FormWindowState.Maximized;

            this.DoubleBuffered = true;

            form1 = this;

            w = System.Windows.Forms.Screen.GetBounds(this).Width;
            h = System.Windows.Forms.Screen.GetBounds(this).Height;

            New();
        }

        public static Form1 form1;

        public static int w;
        public static int h;
        public static int size = 3 + Calculate.arr_size * Calculate.arr_size * 2 + New_Items.max_itemslength + 1;

        public void New()
        {
            di = new Draw_Items(form1);
            game = new Game(form1);
            rects = new Rectangles(form1);
            items = new New_Items(form1);
            calc = new Calculate(form1);
            ls = new Lines(form1);
            ai = new Add_Items(form1);
            draw = new Draw(form1);
            ll = new Light_Lines(form1);
            mga = new Move_Game_Area(form1);

            Start();

            ci = new Control_Items(form1);
        }

        public Game game;
        public Rectangles rects;
        public New_Items items;
        public Calculate calc;
        public Lines ls;
        public bool single = false;
        public Draw_Items di;
        public Add_Items ai;
        public Draw draw;
        public Light_Lines ll;
        public Move_Game_Area mga;
        public Control_Items ci;

        private void Start()
        {
            game.Play();
        }
    }
}



InnerException:

Quote:

System.NullReferenceException: Object reference not set to an instance of an object.
at Game_X_O.Data..cctor() in c:\Users\Ziya1995\Desktop\Game X-O 18 - Copy\Game X-O\Data.cs:line 42

解决方案

OK, lets go through this. A Static Initializer failure is bodybag material. The type is then useless for the life of the process. It's caused by the static constructor throwing an exception. If you set the debugger to break on first chance exceptions, that'll show you the line.

My bet is it's this:

public static int i4_size = i3_size + Form1.form1.items.itemslength;


Which whilst being inline is set implicitly in the constructor. Static constructors get called the first time the type is 'mentioned' in the execution of code. Here you have one thing with unclear lifetime referencing another thing with unclear lifetime.

Can you not use normal instances instead, such that you can be sure they are instantiated in the correct order?


这篇关于静态类:'***'的类型初始值设定项引发了异常。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆