Ejemplo n.º 1
0
        public static ICollection <T> ToEnumerable <T>(this DataTable source) where T : new()
        {
            if (source == null)
            {
                return(null);
            }

            // Init objects
            List <T> items = new List <T>();
            TableFieldNameAttribute attribut = null;
            var properties = typeof(T).GetProperties();

            T item;

            foreach (DataRow row in source.Rows)
            {
                // Create a new instance of the desired object.
                item = (T)Activator.CreateInstance(typeof(T));
                foreach (var property in properties)
                {
                    // Get the attribute.
                    attribut = property
                               .GetCustomAttributes(typeof(TableFieldNameAttribute), true)
                               .FirstOrDefault() as TableFieldNameAttribute;

                    // If there is no attribut, we need to skip the property.
                    if (attribut == null)
                    {
                        continue;
                    }

                    if (source.Columns.Contains(attribut.Name))
                    {
                        if (typeof(IEnumerable).IsAssignableFrom(property.PropertyType) &&
                            property.PropertyType.IsNonStringEnumerable())
                        {
                            // TODO : Handle IEnumerable object.
                            // If you want to help, please make a pull request (I will reference you in my article also). :)
                        }
                        else
                        {
                            // Set the desired object with the value from the row.
                            if (property.PropertyType == typeof(decimal) &&
                                row[attribut.Name].ToString() == string.Empty)
                            {
                                property.SetValue(item, Convert.ChangeType(0, property.PropertyType), null);
                            }
                            else
                            {
                                property.SetValue(item, Convert.ChangeType(row[attribut.Name], property.PropertyType), null);
                            }
                        }
                    }
                }
                items.Add(item);
            }

            return(items);
        }
Ejemplo n.º 2
0
        public static DataTable ToDataTable(this IEnumerable source, Type type)
        {
            if (source == null)
            {
                return(null);
            }

            // Get the attribut name on the class to define the table name.
            TableFieldNameAttribute attribut = type
                                               .GetCustomAttributes(typeof(TableFieldNameAttribute), true)
                                               .FirstOrDefault() as TableFieldNameAttribute;
            var dataTable  = new DataTable(attribut != null ? attribut.Name : string.Empty);
            var properties = type.GetProperties();

            // Create the columns.
            foreach (var property in properties)
            {
                attribut = property
                           .GetCustomAttributes(typeof(TableFieldNameAttribute), true)
                           .FirstOrDefault() as TableFieldNameAttribute;
                if (attribut == null)
                {
                    continue;
                }
                if (property.PropertyType == typeof(bool))
                {
                    dataTable.Columns.Add(attribut.Name, typeof(bool));
                }
                else
                {
                    dataTable.Columns.Add(attribut.Name);
                }
            }

            // Insert the rows.
            object[] values;
            int      valuesIndex = 0;

            foreach (var item in source)
            {
                // Create a temporary object which represent one row.
                values = new object[dataTable.Columns.Count];
                for (int i = 0; i < properties.Length; i++)
                {
                    if (properties[i].GetCustomAttributes(typeof(TableFieldNameAttribute), true).FirstOrDefault() != null)
                    {
                        if (typeof(IEnumerable).IsAssignableFrom(properties[i].PropertyType) &&
                            properties[i].PropertyType.IsNonStringEnumerable())
                        {
                            // TODO : We have an IEnumerable object and we need to do some recursivity here.
                            // I just started the code but it's not finished and it's not working.
                            // If you want to help, please make a pull request (I will reference you in my article also).

                            // The current item is an Enumerable source, so we need a custom process.
                            // Store the current IEnumerable property into a variable.
                            var propertyEnumerable = properties[i].GetValue(item, null);
                            // Get the type of the IEnumerable.
                            var typeOfEnumerable = propertyEnumerable.GetType().GetGenericArguments()[0];
                            // Use some recursivity to transform the IEnumerable to a DataTable.
                            values[valuesIndex] = ((IEnumerable)propertyEnumerable).ToDataTable(typeOfEnumerable);
                        }
                        else
                        {
                            // Fulfill the temporary object with all the values from the item in the enumerable.
                            values[valuesIndex] = properties[i].GetValue(item, null);
                        }
                        // Custom index because we want to avoid the properties which don't have the attribut.
                        valuesIndex++;
                    }
                }
                // Add a new row with the temporary object
                dataTable.Rows.Add(values);
                valuesIndex = 0;
            }

            // When AcceptChanges is called, any DataRow object still in edit mode successfully ends its edits
            dataTable.AcceptChanges();
            return(dataTable);
        }