Ejemplo n.º 1
0
        public static Dictionary <string, Type> GetListProperties(dynamic list)
        {
            var type  = ZGeneric.GetGenericType(list);
            var names = new Dictionary <string, Type>();

            if (ZGeneric.IsDynamicType(type))
            {
                if (list.Count > 0)
                {
                    foreach (var item in ZGeneric.GetDictionaryValues(list[0]))
                    {
                        names.Add(item.Key, (item.Value ?? string.Empty).GetType());
                    }
                }
            }
            else
            {
                foreach (var p in ZReflection.GetProperties(type))
                {
                    names.Add(p.Value.Name, p.Value.PropertyType);
                }
            }

            return(names);
        }
Ejemplo n.º 2
0
        public static DataTable ListToDataTable(object datas, string tableName)
        {
            Type type = ZGeneric.GetGenericType(datas);

            if (string.IsNullOrEmpty(tableName))
            {
                tableName = type.Name;
            }

            DataTable table = new DataTable(tableName);

            table.BeginLoadData();

            var properties = ZReflection.GetProperties(type);

            foreach (var p in properties)
            {
                Type   colType  = p.Value.PropertyType;
                string typeName = colType.ToString();
                if (colType.IsGenericType)
                {
                    typeName = colType.GetGenericArguments()[0].ToString();
                }

                Type newType = Type.GetType(typeName, false);
                if (newType != null)
                {
                    table.Columns.Add(p.Value.Name, newType);
                }
            }

            IEnumerator enumerator = ((dynamic)datas).GetEnumerator();

            while (enumerator.MoveNext())
            {
                DataRow row = table.NewRow();
                foreach (var p in properties)
                {
                    var value = ZGeneric.GetValue(enumerator.Current, p.Value.Name);
                    if ((Type.GetType(p.Value.PropertyType.ToString(), false) != null) && (value != null))
                    {
                        row[p.Value.Name] = value;
                    }
                }
                table.Rows.Add(row);
            }
            table.EndLoadData();
            table.AcceptChanges();
            return(table);
        }
Ejemplo n.º 3
0
        public static IDictionary <string, object> GetDictionaryValues(object item)
        {
            if (IsDynamicType(item.GetType()))
            {
                return(item as IDictionary <string, object>);
            }

            var expando    = (IDictionary <string, object>) new ExpandoObject();
            var properties = ZReflection.GetProperties(item.GetType());

            foreach (var p in properties)
            {
                expando.Add(p.Value.Name, p.Value.GetValue(item, null));
            }
            return(expando);
        }