![](/img/trans.png)
[英]Error : The Type Initializer of Daemon.Global threw an exception in c#
[英]C# Error "The type initializer for … threw an exception
仅在某些计算机上会发生此错误。 通过读取堆栈信息,当我在静态类中调用此静态方法(“ 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)
类型初始化程序异常表示无法创建类型。 当您简单地引用该类时,通常会在调用方法之前发生这种情况。
您这里的代码是您类型的完整文本吗? 我会寻找像任务失败的东西。 通过获得应用程序设置和类似性质的东西,我看到了很多。
static class RHelper
{
//If this line of code failed, you'd get this error
static string mySetting = Settings.MySetting;
}
您还可以通过类型的静态构造函数看到这一点。
无论如何,这个班还有更多吗?
我有同样的错误,但我的情况是由平台目标设置不匹配引起的。 一个库专门设置为x86,而主应用程序设置为“ Any” ...然后我将开发工作移至x64笔记本电脑。
修改Nlog配置文件且未正确格式化XML时,出现此错误。
我尝试了您的代码:
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中的项目是对象类型。
如果类试图获取web.config
不存在的键的值,则会发生此问题。
例如,该类具有静态变量ClientID
private static string ClientID = System.Configuration.ConfigurationSettings.AppSettings["GoogleCalendarApplicationClientID"].ToString();
但是web.config
不包含'GoogleCalendarApplicationClientID'键,则该错误将在任何静态函数调用或任何类实例创建时引发
尝试登录到不再存在的NLog目标时出现此错误。
如果您有Web服务,请检查指向该服务的URL 。 我有一个类似的问题,该问题在更改Web服务URL时已得到解决。
这可能是由于没有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"/>
我用自己的代码遇到此错误。 我的问题是我在配置文件中有重复的密钥。
我遇到了这个问题,就像Anderson Imes所说的,这与应用程序设置有关。 我的问题是,当我应该将其中一项设置的范围设置为“用户”时,它应该是“应用程序”。
此错误是由于我使用了格式错误的NLog.config文件而产生的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.