调试模式下从C#中的方法返回数据表时发生AccessViolationException

调试模式下从C#中的方法返回数据表时发生AccessViolationException,c#,.net,mstest,C#,.net,Mstest,我有一个测试方法,它使用特定的模式构建一个数据表,以便在测试期间使用。如果我内联用于构建DataTable的代码,那么测试可以在调试模式下正常运行。如果我将它封装在一个函数中,当测试在调试模式下运行时,它会抛出AccessViolationException。在发布模式下运行时,它会成功,不会出现问题。我已经包括了下面的代码。我试着设置一些编译器开关和测试环境设置,认为它们是原因,但没有效果。我正在使用MSTest和VS 2012 RC。测试项目是.NET4.5 [TestMethod]

我有一个测试方法,它使用特定的模式构建一个数据表,以便在测试期间使用。如果我内联用于构建DataTable的代码,那么测试可以在调试模式下正常运行。如果我将它封装在一个函数中,当测试在调试模式下运行时,它会抛出AccessViolationException。在发布模式下运行时,它会成功,不会出现问题。我已经包括了下面的代码。我试着设置一些编译器开关和测试环境设置,认为它们是原因,但没有效果。我正在使用MSTest和VS 2012 RC。测试项目是.NET4.5

[TestMethod]
        public void PkColumnSortedToTopWhenColumnMappingIsExisting()
        {
            // arrange
            var sourceMart = new TableMap();
            //var schema = new DataTable();

            var schema = GetDataBaseSchemaTable();


            schema.Rows.Add("Column1", 1, 2, 3, 4, true, /* isKey: */ false, true, "BaseColumn1", "BaseTable1", typeof(int), 24, true, false,
                                        false, false, false);
            schema.Rows.Add("Column2", 1, 2, 3, 4, true, /* isKey: */ false, true, "BaseColumn2", "BaseTable1", typeof(int), 24, true, false,
                                        false, false, false);
            var columnMapping1 = new ColumnMapping(schema.Rows[0], DatabaseType.MicrosoftSql);
            var columnMapping2 = new ColumnMapping(schema.Rows[1], DatabaseType.MicrosoftSql);
            sourceMart.Mappings.Add(columnMapping1);
            sourceMart.Mappings.Add(columnMapping2);
            var viewModel = new MappingViewModel(sourceMart);

            // Navigate to next page (first time building mappings)
            viewModel.Activate();

            // Set second column to be a PK
            sourceMart.Mappings.First(m => m.SourceColumnName == "Column2").IsDestinationColumnPartOfPrimaryKey = true;

            //Reactivate which simulates as if we navigated away and back which should resort it
            viewModel.Activate();

            // assert
            Assert.IsTrue(viewModel.ColumnMappingVMs.First().ColumnMapping.IsDestinationColumnPartOfPrimaryKey);
        }

        public DataTable GetDataBaseSchemaTable()
        {
            var schemaTable = new DataTable();
            schemaTable.Columns.Add(new DataColumn("ColumnName", typeof(string)));
            schemaTable.Columns.Add(new DataColumn("ColumnOrdinal", typeof(int)));
            schemaTable.Columns.Add(new DataColumn("ColumnSize", typeof(int)));
            schemaTable.Columns.Add(new DataColumn("SourceNumericPrecision", typeof(short)));
            schemaTable.Columns.Add(new DataColumn("SourceNumericScale", typeof(short)));
            schemaTable.Columns.Add(new DataColumn("IsUnique", typeof(bool)));
            schemaTable.Columns.Add(new DataColumn("IsKey", typeof(bool)));
            schemaTable.Columns.Add(new DataColumn("IsRowID", typeof(bool)));
            schemaTable.Columns.Add(new DataColumn("BaseColumnName", typeof(string)));
            schemaTable.Columns.Add(new DataColumn("BaseTableName", typeof(string)));
            schemaTable.Columns.Add(new DataColumn("DataType", typeof(Type)));
            schemaTable.Columns.Add(new DataColumn("ProviderType", typeof(int)));
            schemaTable.Columns.Add(new DataColumn("AllowDBNull", typeof(bool)));
            schemaTable.Columns.Add(new DataColumn("IsAliased", typeof(bool)));
            schemaTable.Columns.Add(new DataColumn("IsExpression", typeof(bool)));
            schemaTable.Columns.Add(new DataColumn("IsHidden", typeof(bool)));
            schemaTable.Columns.Add(new DataColumn("IsReadOnly", typeof(bool)));
            schemaTable.Columns.Add(new DataColumn("IsLong", typeof(bool)));

            return schemaTable;
        }

根据,当您尝试读取/写入受保护的内存(或损坏的内存)时,会发生错误。上面的Microsoft链接对此问题有一些有趣的评论。两种模式之间也有相当大的差异,这可能解释了为什么它可以在一种模式下工作,而不能在另一种模式下工作。您是否尝试过安装.NET4.5的官方版本?另外,您是否尝试过启用和禁用它的JIT优化?