Ejemplo n.º 1
0
        public static DataTable ConvertToDataTable <T>(IList <T> list, bool absoluteColumnNames)
        {
            DataTable table      = CreateTable <T>(absoluteColumnNames);
            Type      entityType = typeof(T);
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entityType);

            foreach (T entity in list)
            {
                DataRow row = table.NewRow();

                foreach (PropertyDescriptor propertyDescriptor in properties)
                {
                    PropertyInfo primaryKeyPropertyInfo = EntitiesUtil.GetPrimaryKeyPropertyInfo(propertyDescriptor.PropertyType);
                    object       value = entity.GetType().GetProperty(propertyDescriptor.Name).GetValue(entity, null);

                    if (primaryKeyPropertyInfo != null)
                    {
                        if (value != null)
                        {
                            object primaryKeyValue = value.GetType().GetProperty(primaryKeyPropertyInfo.Name).GetValue(value, null);

                            if (absoluteColumnNames)
                            {
                                row[propertyDescriptor.Name + "." + primaryKeyPropertyInfo.Name] = primaryKeyValue;
                            }
                            else
                            {
                                row[primaryKeyPropertyInfo.Name] = primaryKeyValue;
                            }
                        }
                        else
                        {
                            row[primaryKeyPropertyInfo.Name] = DBNull.Value;
                        }
                    }
                    else
                    {
                        row[propertyDescriptor.Name] = propertyDescriptor.GetValue(entity) ?? DBNull.Value;
                    }
                }

                table.Rows.Add(row);
            }

            return(table);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Crea una Tabla sencilla a partir de la Definición de una Entidad del Framework.
        /// </summary>
        /// <typeparam name="T">Tipo de la Entidad.</typeparam>
        /// <returns>Tabla con un Esquema acorde a <c>T</c>.</returns>
        public static DataTable CreateTable <T>(bool absoluteColumnNames)
        {
            Type entityType = typeof(T);

            DataTable table = new DataTable(entityType.Name);
            PropertyDescriptorCollection entityPropertiesCollection = TypeDescriptor.GetProperties(entityType);

            foreach (PropertyDescriptor entityProperty in entityPropertiesCollection)
            {
                PropertyInfo primaryKeyType = EntitiesUtil.GetPrimaryKeyPropertyInfo(entityProperty.PropertyType);
                if (primaryKeyType != null)
                {
                    if (absoluteColumnNames)
                    {
                        table.Columns.Add(entityProperty.Name + "." + primaryKeyType.Name,
                                          primaryKeyType.PropertyType);
                    }
                    else
                    {
                        table.Columns.Add(primaryKeyType.Name, primaryKeyType.PropertyType);
                    }
                }
                else if (entityProperty.PropertyType.IsGenericType &&
                         entityProperty.PropertyType.GetGenericTypeDefinition()
                         .Equals(typeof(Nullable <>)))
                {
                    table.Columns.Add(entityProperty.Name, Nullable.GetUnderlyingType(entityProperty.PropertyType));
                }
                else
                {
                    table.Columns.Add(entityProperty.Name, entityProperty.PropertyType);
                }
            }

            return(table);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Obtiene las Propiedades de la Entidad que son consideradas para formar la Llave
 /// Primaria.
 /// </summary>
 /// <param name="entityType">Tipo de la Entidad.</param>
 /// <returns>Propiedades para Llave Primaria.</returns>
 public static PropertyInfo[] GetPrimaryKeyProperties(Type entityType)
 {
     return(EntitiesUtil.GetPropertiesWithSpecificAttribute(entityType, typeof(PrimaryKey)));
 }