예제 #1
0
        // This method reads the attributes of your container class via
        // reflection  in order to build a schema for the DataTable that you
        // will explicitly convert to.
        private DataTable ConstructDataTableSchema(T item)
        {
            string tableName = string.Empty;
            List <DTConverterContainer> schemaContainers = new List <DTConverterContainer>();
            Type type = item.GetType();

            MemberInfo[] members = type.GetProperties();
            foreach (MemberInfo member in members)
            {
                object[] attributes = member.GetCustomAttributes(true);
                if (attributes.Length == 0)
                {
                    continue;
                }
                foreach (object attribute in attributes)
                {
                    ConversionAttribute ca = attribute as ConversionAttribute;
                    if (ca == null)
                    {
                        continue;
                    }
                    if (!ca.DataTableConversion)
                    {
                        continue;
                    }
                    // The name of the container class is used to name
                    // your DataTable

                    string[] classNameArray =
                        member.ReflectedType.ToString().Split(
                            Convert.ToChar("."));
                    tableName = classNameArray[classNameArray.Length - 1];
                    string       name      = member.Name;
                    PropertyInfo prop      = type.GetProperty(name);
                    Type         valueType = prop.GetValue(item, null).GetType();
                    // Each property that is  will be a column in our
                    // DataTable.

                    schemaContainers.Add(new DTConverterContainer(name,
                                                                  valueType, ca.AllowDBNull, ca.KeyField));
                }
            }
            if (schemaContainers.Count > 0)
            {
                DataTable    dataTable  = new DataTable(tableName);
                DataColumn[] dataColumn = new DataColumn[schemaContainers.Count];
                // Counts the number of keys that will need to be created

                int totalNumberofKeys = 0;
                foreach (DTConverterContainer container in schemaContainers)
                {
                    if (container.IsKey && m_enforceKeys)
                    {
                        totalNumberofKeys = totalNumberofKeys + 1;
                    }
                }
                // Builds the DataColumns for our DataTable

                DataColumn[] keyColumnArray = new DataColumn[totalNumberofKeys];
                int          keyColumnIndex = 0;
                for (int i = 0; i < schemaContainers.Count; i++)
                {
                    dataColumn[i] = new DataColumn
                    {
                        DataType    = schemaContainers[i].PropertyType,
                        ColumnName  = schemaContainers[i].PropertyName,
                        AllowDBNull = schemaContainers[i].AllowDBNull
                    };
                    dataTable.Columns.Add(dataColumn[i]);
                    if (schemaContainers[i].IsKey != true || m_enforceKeys != true)
                    {
                        continue;
                    }
                    keyColumnArray[keyColumnIndex] = dataColumn[i];
                    keyColumnIndex = keyColumnIndex + 1;
                }
                if (m_enforceKeys)
                {
                    dataTable.PrimaryKey = keyColumnArray;
                }
                return(dataTable);
            }
            return(null);
        }
예제 #2
0
        public DataTable GetDataTable(List <T> items)
        {
            DataTable dt;

            try
            {
                // Build a table schema from the first element in the collection

                dt = ConstructDataTableSchema(items[0]);
            }
            catch (IndexOutOfRangeException ex)
            {
                throw (new ApplicationException(
                           "Cannot convert List of zero length to a DataTable", ex));
            }
            // If the container is not convertable than throw an

            // ApplicationException.

            if (dt == null)
            {
                throw new ApplicationException("List items are not convertable.");
            }
            // Create a new row for every item in the collection and fill it.
            for (int i = 0; i < items.Count; i++)
            {
                DataRow      dr      = dt.NewRow();
                Type         type    = items[i].GetType();
                MemberInfo[] members = type.GetProperties();

                foreach (MemberInfo member in members)
                {
                    object[] attributes = member.GetCustomAttributes(true);
                    if (attributes.Length == 0)
                    {
                        continue;
                    }
                    foreach (object attribute in attributes)
                    {
                        ConversionAttribute ca = attribute as
                                                 ConversionAttribute;
                        if (ca == null)
                        {
                            continue;
                        }
                        if (!ca.DataTableConversion)
                        {
                            continue;
                        }
                        string[] nameArray
                            = member.Name.Split(
                                  Convert.ToChar(" "));
                        PropertyInfo prop = type.GetProperty(
                            nameArray[0]);
                        //                       Type valueType = prop.GetValue(items[i],
                        //                                                      null).GetType() // COMMENTED BY CODEIT.RIGHT;
                        dr[nameArray[0]] = prop.GetValue(items[i], null);
                    }
                }

                dt.Rows.Add(dr);
            }
            return(dt);
        }