C#错误 "类型初始化程序为...抛出了异常"

37

这个错误只在一些计算机上出现。通过查看堆栈信息,发现在调用静态类中的静态方法(“FormatQuery”)时存在某些问题:

using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using FlexCel.Report;
using FlexCel.XlsAdapter;
using ComboBox=System.Windows.Forms.ComboBox;

namespace XSoftArt.A
{
    static class RHelper
    {
        private static string FormatQuery(string FieldName, int Count,
            CheckedListBox chekedListBox)
        {
            string ID = string.Empty;
            int n = Count;

            foreach (DataRowView item in chekedListBox.CheckedItems)
            {
                ID = ID + item["" + FieldName + ""];
                if (n > 1)
                {
                    ID = ID + " , ";
                    n--;
                }
            }
            return ID;
        }

        public static string FormatQuery(CheckedListBox chekedListBox)
        {
            return FormatQuery(chekedListBox.ValueMember,
                chekedListBox.CheckedItems.Count, chekedListBox);
        }
    }

那么,问题是什么?我该如何解决?项目配置有问题还是调试模式出了差错或其他原因?

错误信息:

   at XSoftArt.EVS.ReportHelper.FormatQuery(CheckedListBox chekedListBox)
   at XSoftArt.EVS.NewEmailSelectClient.LoadList_v2(String search, TextBox txtbox)
   at XSoftArt.EVS.NewEmailSelectClient.LoadContacts()
   at XSoftArt.EVS.NewEmailSelectClient.button7_Click(Object sender, EventArgs e)
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

4
它是否指出它抛出了哪个异常? - Lasse V. Karlsen
3
请检查InnerException和详细信息。 - annakata
2
那个堆栈跟踪中没有提到FormatQuery,你确定是这个类有问题吗?这个类在问题中是完整的还是你遗漏了什么? - Lasse V. Karlsen
1
抱歉,我又编辑了一次我的帖子,并再次粘贴了错误信息。 - Vytas
在出现故障的机器上,您是否缺少一个被引用的 DLL? - user773300
显示剩余5条评论
12个回答

64

类型初始化程序异常表示无法创建该类型。这通常会在你引用该类时、调用方法之前发生。

这里的代码是否是你的类型的完整文本?我会查找类似于赋值失败的东西。我经常在获取应用程序设置之类的内容时遇到这种情况。

static class RHelper
{
     //If this line of code failed, you'd get this error
     static string mySetting = Settings.MySetting;
} 

您还可以通过类型的静态构造函数查看这一点。

无论如何,这个类还有什么其他的吗?


10
这正是我遇到的问题 - 对于静态成员的赋值直到构造函数被调用后才发生。谢谢! - Mike
天才答案...这正是我的问题,我也觉得很难调试,因为异常掩盖了真正的错误。谢谢你的帮助。 - BG100

8

我也遇到了同样的错误,但是这是因为平台目标设置不匹配造成的。其中一个库设置为 x86,而主应用程序设置为“任何”……然后我将我的开发转移到了一台 x64 笔记本电脑上。


8

如果一个类试图获取web.config中不存在的键的值,就会出现这个问题。

例如,该类具有静态变量ClientID

private static string ClientID = System.Configuration.ConfigurationSettings.AppSettings["GoogleCalendarApplicationClientID"].ToString();

但是如果web.config文件中没有包含“GoogleCalendarApplicationClientID”键,则会在任何静态函数调用或任何类实例创建时抛出错误。


5

当我修改了一个Nlog配置文件并且没有正确格式化XML时,就会出现这个错误。


4
我尝试了你的代码:
CheckedListBox cb = new CheckedListBox();
for (var i = 1; i < 11; i++)
  cb.Items.Add("Item " + i, i % 3 == 0);

string fmt = RHelper.FormatQuery(cb);
Console.WriteLine(fmt);
Console.ReadLine();

在这一行代码中出现了异常:

foreach (DataRowView item in chekedListBox.CheckedItems)

// Unable to cast object of type 'System.String' to type 'System.Data.DataRowView'.

也许你也遇到了同样的问题。不要将其转换为 DataRowView,尝试进行以下更改:
foreach (var item in chekedListBox.CheckedItems)
{
    ID = ID + item.ToString(); // item["" + FieldName + ""];

因为CheckedListBox中的项目是对象类型。


我不这么认为,因为这段代码在另一个项目中运行良好。我认为这可能是其他问题(我将此模块从另一个项目复制到此处...)。对该方法的调用看起来像这样(当我为SQL放置参数时):“@StateID”,ReportHelper.FormatQuery(db_InvoiceStateID)。这里的db_InvoiceStateID是一个checkedListBox组件。 - Vytas

1
这可能是由于没有 Oracle Client 的管理员权限所致。请在 App.config 文件中添加以下内容:
<IPermission class="Oracle.DataAccess.Client.OraclePermission,
 Oracle.DataAccess, Version=2.111.7.20, Culture=neutral,
 PublicKeyToken=89b483f429c47342" version= "1" Unrestricted="true"/>

1
如果您有Web服务,请检查指向该服务的URL。当我更改了我的Web服务URL时,我遇到了类似的问题,这个问题得到了解决。

1

我在自己的代码中遇到了这个错误。我的问题是配置文件中有重复的键。


1

当我尝试登录到一个不存在的NLog目标时,出现了这个错误。


0

我遇到了这个问题,就像Anderson Imes所说,它与应用设置有关。我的问题是我的一个设置的范围被设置为“用户”,而应该是“应用程序”。


那么你去哪里改变这个了?细节,伙计,细节! - vapcguy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接