Пример #1
0
        private static object PopulateObject(Type t, RowData row)
        {
            object instance = Activator.CreateInstance(t);

            foreach (string prop in row.GetDynamicMemberNames())
            {
                PropertyInfo p = instance.GetType().GetProperty(prop);
                if (p != null && p.CanWrite)
                {
                    object o = row.Column(prop);
                    if (o is ResultTable && p.PropertyType.GetInterface("IList", true) != null)
                    {
                        Type   listType     = typeof(List <>);
                        Type[] args         = p.PropertyType.GetGenericArguments();
                        Type   genericList  = listType.MakeGenericType(args);
                        object listInstance = Activator.CreateInstance(genericList);
                        foreach (RowData r in (o as ResultTable))
                        {
                            ((IList)listInstance).Add(PopulateObject(args[0], r));
                        }
                        p.SetValue(instance, listInstance);
                    }
                    else
                    {
                        p.SetValue(instance, o);
                    }
                }
            }
            return(instance);
        }