仅在某些计算机上会发生此错误。 通过读取堆栈信息,当我在静态类中调用此静态方法(" 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时,出现此错误。
如果类试图获取
例如,该类具有静态变量
private static string ClientID = System.Configuration.ConfigurationSettings.AppSettings["GoogleCalendarApplicationClientID"].ToString();
但
我尝试了您的代码:
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中的项目属于对象类型。
这可能是由于没有
<IPermission class="Oracle.DataAccess.Client.OraclePermission,
Oracle.DataAccess, Version=2.111.7.20, Culture=neutral,
PublicKeyToken=89b483f429c47342" version="1" Unrestricted="true"/>
尝试登录到不再存在的NLog目标时出现此错误。
我用自己的代码遇到此错误。我的问题是我在配置文件中有重复的密钥。
如果您有Web服务,请检查指向该服务的URL。我有一个类似的问题,该问题在更改Web服务URL时已得到解决。
我遇到了这个问题,就像Anderson Imes所说的,这与应用程序设置有关。我的问题是,当我应该将其中一项设置的范围设置为"用户"时,它应该是"应用程序"。
该错误是由于我使用了格式错误的NLog.config文件而产生的。
以上就是短码网小编为大家整理的《类 C#错误“…的类型初始值设定项引发了异常》相关内容,希望大家喜欢。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权/违法违规/事实不符,请将联系本站反馈,一经查实,立即处理!
《类 C#错误“…的类型初始值设定项引发了异常》文档下载仅供参考学习,下载后请在24小时内删除。
转载注明出处:https://www.duanma.net/article/6032a339526.html