• ToDataTable v2


    DynamicObject & ExpandoObject should be accessed in different way.
     
     
            public static DataTable ToDataTable<T>( IEnumerable<T> input, string tableName)
            {
                var dt = new DataTable();
                dt.TableName = tableName;
     
                if (input == null || !input.Any()) return dt;
                if (typeof (T) == typeof(System. Object))
                {
                    //"T is dynamic type"
                    var first = Enumerable .First(input);
                    if (first is IDictionary< string, object >)  //ExpandoObject also falls into this category         
                    {
                        IDictionary<string , object> properties = ( IDictionary<string , object>)first;
     
                        foreach (var p in properties)
                        {
                            var type = p.Value.GetType();
                            if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>))
                                type = type.GetGenericArguments()[0];
     
                            dt.Columns.Add(p.Key, type);
                        }
     
                        foreach (T r in input)
                        {
                            var values = new object[properties.Count];
                            int i = 0;
                            foreach (var p in properties)
                            {
                                values[i] = (r as IDictionary <string, object>)[p.Key];
                                i++;
                            }
     
                            dt.LoadDataRow(values, true);
                        }
                    }
                   
                    else if (first is System.Dynamic. DynamicObject)
                    {
                        var firstDO = first as System.Dynamic.DynamicObject;
                        var properties = firstDO.GetDynamicMemberNames();
     
                        foreach (var p in properties)
                        {
                            var pObject = GetDynamicMember(first, p);
                            var type = pObject.GetType();
                            if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>))
                                type = type.GetGenericArguments()[0];
     
                            dt.Columns.Add(p, type);
                        }
     
                        foreach (T r in input)
                        {
                            var values = new object[properties.Count()];
                            int i = 0;
                            foreach (var p in properties)
                            {
                                values[i] = GetDynamicMember(r, p);                               
                                i++;
                            }
     
                            dt.LoadDataRow(values, true);
                        }
                    }
     
                }
                else
                {
                    // T is static type               
                    var properties = typeof (T).GetProperties();
     
                    foreach (var p in properties)
                    {
                        var type = p.PropertyType;
                        if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>))
                            type = type.GetGenericArguments()[0];
     
                        dt.Columns.Add(p.Name, type);
                    }
     
                    foreach (T r in input)
                    {
                        var values = new object[properties.Length];
                        int i = 0;
                        foreach (var p in properties)
                        {
                            values[i] = p.GetValue(r, null);
                            i++;
                        }
     
                        dt.LoadDataRow(values, true);
                    }
                }
                return dt;
            }
           
            //todo cache the callsite
            private static object GetDynamicMember( object obj, string memberName)
            {
                var binder = Binder .GetMember(CSharpBinderFlags.None, memberName, obj.GetType(),
                    new[] { CSharpArgumentInfo .Create(CSharpArgumentInfoFlags.None, null) });
                var callsite = CallSite <Func< CallSite, object , object>>.Create(binder);
                return callsite.Target(callsite, obj);
            }

  • 相关阅读:
    抓取猫眼电影top100的正则、bs4、pyquery、xpath实现方法
    Win实用好用软件清单推荐
    Manjaro安装配置美化字体模糊发虚解决记录
    爬取杭电oj所有题目
    Python爬取微博热搜以及链接
    20191225_Python构造函数知识以及相关注意事项
    java_细节_windows7下记事本保存为utf-8格式的问题
    基础_划分子网
    爬虫_爬取有道每日一句
    算法_基础_伪代码定义以及遵循的规则
  • 原文地址:https://www.cnblogs.com/gunsmoke/p/2501445.html
Copyright © 2020-2023  润新知