1

我正在尝试使用 NuGet 包 FastMember 将列表对象加载到 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。

4

1 回答 1

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; }
    }
}
于 2013-11-06T05:39:00.157 回答