1

有没有办法ObjectReader.CreateExpandoObject.

这是我用来测试的一个简单的测试用例:

using FastMember;
using System;
using System.Collections.Generic;
using System.Data;
using System.Dynamic;
using System.Linq;

namespace TestReaderAsync
{
    class Program
    {
        static void Main()
        {
            List<ExpandoObject> expandos = new List<ExpandoObject>();
            var first = GetExpando();
            var second = GetExpando();
            expandos.Add(first);
            expandos.Add(second);
            var keys = (first as IDictionary<string, object>).Keys;
            using (var reader = ObjectReader.Create(expandos, keys.ToArray()))
            {
                var dt = new DataTable();
                dt.Load(reader);
            }
        }

        private static ExpandoObject GetExpando()
        {
            ExpandoObject exp = new ExpandoObject();
            exp.TryAdd("ID", "A");
            exp.TryAdd("ID_2", "B");
            exp.TryAdd("DATE", DateTime.Now);
            exp.TryAdd("MONTH", 5);
            return exp;
        }
    }
}

看来问题出GetMembersTypeAccessor. 由于ExpandoObject继承自IDictionary<string, object>,因此该类型没有属性,并且 FastMember 无法序列化字典的每个键。是否有解决方法来实现它?

基本上,我执行翻译成expandos的SQL查询。我想用来ObjectReader将结果批量备份到数据库中。

4

0 回答 0