DataTable.Load()指定的参数超出了有效值的范围

时间:2013-11-06 05:06:55

标签: c# .net list visual-studio-2012 fastmember

我正在尝试使用NuGet Package FastMember将List对象加​​载到DataTable对象中。

以下是我正在使用的代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using FastMember;

namespace ConsoleScratchPad
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] a = { "The", "Big", "Ant" };
            List<string> ls = a.ToList();
            DataTable dt = new DataTable();
            using (var reader = ObjectReader.Create(ls))
            {
                dt.Load(reader);
                     //^--------------------Error
            }
        }
    }
}

在指定的标记处,我收到以下错误。

System.ArgumentOutOfRangeException was unhandled
  HResult=-2146233086
  Message=Specified argument was out of the range of valid values.
Parameter name: name
  Source=FastMember_dynamic
  ParamName=name
  StackTrace:
       at FastMember_dynamic.String_1.get_Item(Object , String )
       at FastMember.ObjectReader.System.Data.IDataRecord.GetValues(Object[] values) in c:\Dev\fast-member\FastMember\ObjectReader.cs:line 300
       at System.Data.ProviderBase.DataReaderContainer.CommonLanguageSubsetDataReader.GetValues(Object[] values)
       at System.Data.ProviderBase.SchemaMapping.LoadDataRow()
       at System.Data.Common.DataAdapter.FillLoadDataRow(SchemaMapping mapping)
       at System.Data.Common.DataAdapter.FillFromReader(DataSet dataset, DataTable datatable, String srcTable, DataReaderContainer dataReader, Int32 startRecord, Int32 maxRecords, DataColumn parentChapterColumn, Object parentChapterValue)
       at System.Data.Common.DataAdapter.Fill(DataTable[] dataTables, IDataReader dataReader, Int32 startRecord, Int32 maxRecords)
       at System.Data.Common.LoadAdapter.FillFromReader(DataTable[] dataTables, IDataReader dataReader, Int32 startRecord, Int32 maxRecords)
       at System.Data.DataTable.Load(IDataReader reader, LoadOption loadOption, FillErrorEventHandler errorHandler)
       at System.Data.DataTable.Load(IDataReader reader)
       at ConsoleScratchPad.Program.Main(String[] args) in f:\Dropbox\Projects\visual studio 2012\Projects\ConsoleScratchPad\ConsoleScratchPad\Program.cs:line 31
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

想知道我在这里做错了什么,以及它的修复方法。

我正在使用带有c#.net控制台应用程序的visual studio 2012。

1 个答案:

答案 0 :(得分:3)

我不熟悉FastMember包,但似乎你不能为数据源使用字符串数组。这将有效:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using FastMember;

namespace ConsoleScratchPad
{
    class Program
    {
        static void Main(string[] args)
        {
            IList<MyClass> ls = new List<MyClass>();
            ls.Add(new MyClass { MyColumn1 = "The" });
            ls.Add(new MyClass { MyColumn1 = "Big" });
            ls.Add(new MyClass { MyColumn1 = "Ant" });
            DataTable dt = new DataTable();
            using (var reader = ObjectReader.Create(ls))
            {
                dt.Load(reader);
            }
        }
    }

    public class MyClass
    {
        public string MyColumn1 { get; set; }
    }
}