带有 EPPlus 脚本的 TypeInitializationException
TypeInitializationException with EPPlus script
我正在尝试 运行 脚本以使用 EPPlus
从 Excel sheet 中读取值并将它们加载到元组列表中。
但是,当我 运行 脚本出现两个错误时,第一个是:
An unhandled exception of type 'System.TypeInitializationException' occurred in mscorlib.dll
我在其他帖子中看到我需要检查内部异常,但是 none 是由 Visual Studio 15 提供的。
这是所有可用的异常详细信息。
System.TypeInitializationException was unhandled
Message: An unhandled exception of type 'System.TypeInitializationException' occurred in mscorlib.dll
Additional information: The type initializer for 'CGCompare2.Program' threw an exception.
然后当我关闭 VS15 异常时 window 我得到一个弹出对话框:
Cannot access a disposed object.
Object name: 'HwndSourceAdapter'
我不确定这是什么问题,是否是由我的代码引起的。任何帮助,非常感谢。
Program.cs
using System;
using System.Collections.Generic;
using System.IO;
using OfficeOpenXml;
namespace CGComparer
{
class Program
{
private static List<Tuple<string, string>> _listTop;
private static List<Tuple<string, string>> _listGNED;
private static Base _baseCell;
private static ExcelPackage _package = new ExcelPackage(new FileInfo(_excelFile));
private static string _excelFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
"Compare GNED and TOP V1.0.xlsx");
static void Main(string[] args)
{
_baseCell = new Base(1, 2);
_listTop = ColumnsToList(_baseCell.Column(),_baseCell.Row());
_baseCell = new Base(3, 2);
_listGNED = ColumnsToList(_baseCell.Column(),_baseCell.Row());
}
public static List<Tuple<string, string>> ColumnsToList(int column, int row)
{
var list = new List<Tuple<string, string>>();
var ws = _package.Workbook.Worksheets[1];
var ListIsValid = true;
do
{
var userEmail = (string)ws.Cells[column, row].Value;
var customerGroup = (string)ws.Cells[column + 1, row].Value;
if (!string.IsNullOrEmpty(userEmail))
{
list.Add(new Tuple<string, string>(userEmail, customerGroup));
row = row++;
}
else
{
ListIsValid = false;
}
} while (ListIsValid);
return list;
}
}
}
Base.cs
namespace CGComparer
{
public class Base
{
private static int _column;
private static int _row;
public Base(int column, int row)
{
_column = column;
_row = row;
}
public int Column()
{
return _column;
}
public int Row()
{
return _row;
}
}
}
所以,事实证明这个问题一直困扰着我,我按照问题评论中的 Hans Passant 提供的以下步骤进行了操作:
"The debugger in VS2015 is a crappy bag 'o bugs, it won't let you look
at the InnerException. Use Tools > Options > Debugging > General >
tick "Use Managed Compatibility Mode" and now you can see it. Careful
with those statics, their initializer can byte you in the rear end
badly."
这是一个空引用异常,由我声明 excel 文件时带有尚未声明的路径参数引起。
private static ExcelPackage _package = new ExcelPackage(new FileInfo(_excelFile));
private static string _excelFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
"Compare GNED and TOP V1.0.xlsx");
我正在尝试 运行 脚本以使用 EPPlus
从 Excel sheet 中读取值并将它们加载到元组列表中。
但是,当我 运行 脚本出现两个错误时,第一个是:
An unhandled exception of type 'System.TypeInitializationException' occurred in mscorlib.dll
我在其他帖子中看到我需要检查内部异常,但是 none 是由 Visual Studio 15 提供的。 这是所有可用的异常详细信息。
System.TypeInitializationException was unhandled
Message: An unhandled exception of type 'System.TypeInitializationException' occurred in mscorlib.dll
Additional information: The type initializer for 'CGCompare2.Program' threw an exception.
然后当我关闭 VS15 异常时 window 我得到一个弹出对话框:
Cannot access a disposed object.
Object name: 'HwndSourceAdapter'
我不确定这是什么问题,是否是由我的代码引起的。任何帮助,非常感谢。
Program.cs
using System;
using System.Collections.Generic;
using System.IO;
using OfficeOpenXml;
namespace CGComparer
{
class Program
{
private static List<Tuple<string, string>> _listTop;
private static List<Tuple<string, string>> _listGNED;
private static Base _baseCell;
private static ExcelPackage _package = new ExcelPackage(new FileInfo(_excelFile));
private static string _excelFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
"Compare GNED and TOP V1.0.xlsx");
static void Main(string[] args)
{
_baseCell = new Base(1, 2);
_listTop = ColumnsToList(_baseCell.Column(),_baseCell.Row());
_baseCell = new Base(3, 2);
_listGNED = ColumnsToList(_baseCell.Column(),_baseCell.Row());
}
public static List<Tuple<string, string>> ColumnsToList(int column, int row)
{
var list = new List<Tuple<string, string>>();
var ws = _package.Workbook.Worksheets[1];
var ListIsValid = true;
do
{
var userEmail = (string)ws.Cells[column, row].Value;
var customerGroup = (string)ws.Cells[column + 1, row].Value;
if (!string.IsNullOrEmpty(userEmail))
{
list.Add(new Tuple<string, string>(userEmail, customerGroup));
row = row++;
}
else
{
ListIsValid = false;
}
} while (ListIsValid);
return list;
}
}
}
Base.cs
namespace CGComparer
{
public class Base
{
private static int _column;
private static int _row;
public Base(int column, int row)
{
_column = column;
_row = row;
}
public int Column()
{
return _column;
}
public int Row()
{
return _row;
}
}
}
所以,事实证明这个问题一直困扰着我,我按照问题评论中的 Hans Passant 提供的以下步骤进行了操作:
"The debugger in VS2015 is a crappy bag 'o bugs, it won't let you look at the InnerException. Use Tools > Options > Debugging > General > tick "Use Managed Compatibility Mode" and now you can see it. Careful with those statics, their initializer can byte you in the rear end badly."
这是一个空引用异常,由我声明 excel 文件时带有尚未声明的路径参数引起。
private static ExcelPackage _package = new ExcelPackage(new FileInfo(_excelFile));
private static string _excelFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
"Compare GNED and TOP V1.0.xlsx");