Пример #1
0
        /// <summary>
        /// Deserializes the specified <see cref="CsvTable"/> with the specified <see cref="Type"/>.
        /// </summary>
        /// <param name="table"><see cref="CsvTable"/> from which the data is deserialize.</param>
        /// <param name="objectType"><see cref="Type"/> of object to deserialize.</param>
        /// <returns>Returns the deserialized object array.</returns>
        public static object[] Deserialize(CsvTable table, Type objectType)
        {
            var rows = table.Rows;
            var properties = objectType.GetProperties();
            var parentObj = (object)null;
            var objList = new List<object>();

            for (int x = 0; x < rows.Count; x++)
            {
                var childObj = Activator.CreateInstance(objectType);
                for (int i = 0; i < properties.Length; i++) // set property value loop
                {
                    var property = properties[i];
                    if (HasIgnoreAttribute(property))
                        continue; // ignore CsvIgnoreAttribute

                    var propertyName = GetPropertyAttributeName(property);
                    var value = rows[x][propertyName];
                    var parameters = new object[] { value };

                    if (parentObj != null && value == DBNull.Value) // get data from parent
                        parameters = new object[] { property.GetMethod.Invoke(parentObj, null) };
                    else if (value == DBNull.Value)
                        continue;

                    var isParent = property.Name == "Name" && value != DBNull.Value;
                    property.SetMethod.Invoke(childObj, parameters);

                    if (isParent)
                        parentObj = childObj;
                }
                objList.Add(childObj);
            }
            return objList.ToArray();
        }
Пример #2
0
        /// <summary>
        /// Deserializes the specified <see cref="CsvTable"/> with the specified <see cref="Type"/>.
        /// </summary>
        /// <param name="table"><see cref="CsvTable"/> from which the data is deserialize.</param>
        /// <param name="objectType"><see cref="Type"/> of object to deserialize.</param>
        /// <returns>Returns the deserialized object array.</returns>
        public static object[] Deserialize(CsvTable table, Type objectType)
        {
            var rows = table.Rows;
            var properties = objectType.GetProperties();
            var parentObj = (object)null;
            var objList = new List<object>();

            for (int x = 0; x < rows.Count; x++)
            {
                var childObj = Activator.CreateInstance(objectType);
                for (int i = 0; i < properties.Length; i++) // set property value loop
                {
                    var property = properties[i];
                    if (HasIgnoreAttribute(property))
                        continue; // ignore CsvIgnoreAttribute

                    var propertyName = GetPropertyAttributeName(property);
                    var value = rows[x][propertyName];
                    var parameters = new object[] { value };

                    if (parentObj != null && value == DBNull.Value) // get data from parent
                        parameters = new object[] { property.GetMethod.Invoke(parentObj, null) };
                    else if (value == DBNull.Value)
                        continue;

                    var isParent = property.Name == "Name" && value != DBNull.Value;
                    property.SetMethod.Invoke(childObj, parameters);

                    if (isParent)
                        parentObj = childObj;
                }
                objList.Add(childObj);
            }
            return objList.ToArray();
        }
Пример #3
0
        /// <summary>
        /// Deserializes the specified <see cref="CsvTable"/> with the specified <see cref="Type"/>.
        /// </summary>
        /// <typeparam name="T">Type with which the <see cref="CsvTable"/> will be deserialized.</typeparam>
        /// <param name="table"><see cref="CsvTable"/> from which the data is deserialize.</param>
        /// <returns>Returns the deserialized object array as the specified type.</returns>
        public static T[] Deserialize <T>(CsvTable table)
        {
            var tType = typeof(T);
            var objs  = Deserialize(table, tType);

            return(Array.ConvertAll(objs, obj => (T)obj));
        }
Пример #4
0
        /// <summary>
        /// Deserializes the specified <see cref="CsvTable"/> with the specified <see cref="Type"/>.
        /// </summary>
        /// <param name="table"><see cref="CsvTable"/> from which the data is deserialize.</param>
        /// <param name="objectType"><see cref="Type"/> of object to deserialize.</param>
        /// <returns>Returns the deserialized object array.</returns>
        public static object[] Deserialize(CsvTable table, Type objectType)
        {
            if (table == null)
                throw new ArgumentNullException("table");
            if (objectType == null)
                throw new ArgumentNullException("objectType");
            if (!objectType.IsSubclassOf(typeof(CoCData)))
                throw new ArgumentException("objectType is not a subclass of type CoCData.");

            var rows = table.Rows;
            var properties = objectType.GetProperties();
            var parentObj = (object)null;
            var objList = new List<object>();
            var id = -1;

            for (int x = 0; x < rows.Count; x++)
            {
                var childObj = Activator.CreateInstance(objectType);
                for (int i = 0; i < properties.Length; i++) // set property value loop
                {
                    var property = properties[i];
                    if (property.DeclaringType != objectType)
                    {
                        property.SetMethod.Invoke(childObj, new object[] { id });
                        continue;
                    }

                    if (HasIgnoreAttribute(property))
                        continue; // ignore CsvIgnoreAttribute

                    var propertyName = GetPropertyAttributeName(property);
                    var value = rows[x][propertyName];
                    var parameters = new object[] { value };

                    if (parentObj != null && value == DBNull.Value) // get data from parent
                        parameters = new object[] { property.GetMethod.Invoke(parentObj, null) };
                    else if (value == DBNull.Value)
                        continue; // keep default value

                    var isParent = property.Name == "Name" && value != DBNull.Value;
                    property.SetMethod.Invoke(childObj, parameters);

                    if (isParent)
                    {
                        id += 1;
                        parentObj = childObj;
                    }
                }
                objList.Add(childObj);
            }
            return objList.ToArray();
        }
Пример #5
0
        private void PrintTable(CsvTable table)
        {
            var strBuilder = new StringBuilder();
            for (int i = 0; i < table.Columns.Count; i++)
                strBuilder.Append(table.Columns[i].ColumnName.PadRight(100));
            strBuilder.AppendLine();

            for (int i = 0; i < table.Rows.Count; i++)
            {
                for (int k = 0; k < table.Rows[i].ItemArray.Length; k++)
                    strBuilder.Append(table.Rows[i].ItemArray[k].ToString().PadRight(100));
                strBuilder.AppendLine();
            }
            Console.WriteLine(strBuilder.ToString());
        }
Пример #6
0
        private void WriteTableToTxt(CsvTable table, string path)
        {
            var strBuilder = new StringBuilder();
            for (int i = 0; i < table.Columns.Count; i++)
                strBuilder.Append(table.Columns[i].ColumnName.PadRight(100));
            strBuilder.AppendLine();

            for (int i = 0; i < table.Rows.Count; i++)
            {
                for (int k = 0; k < table.Rows[i].ItemArray.Length; k++)
                    strBuilder.Append(table.Rows[i].ItemArray[k].ToString().PadRight(100));
                strBuilder.AppendLine();
            }
            File.WriteAllText(path, strBuilder.ToString());
        }
Пример #7
0
 public void TestUncompressedCsvTable()
 {
     var table = new CsvTable("Resources/characters.csv");
     //WriteTableToTxt(table, "characters_parsed.txt");
     //table.Save("saved_characters.csv");
 }
Пример #8
0
        /// <summary>
        /// Deserializes the specified <see cref="CsvTable"/> with the specified <see cref="Type"/>.
        /// </summary>
        /// <param name="table"><see cref="CsvTable"/> from which the data is deserialize.</param>
        /// <param name="objectType"><see cref="Type"/> of object to deserialize.</param>
        /// <returns>Returns the deserialized object array.</returns>
        public static object[] Deserialize(CsvTable table, Type objectType)
        {
            if (table == null)
            {
                throw new ArgumentNullException("table");
            }
            if (objectType == null)
            {
                throw new ArgumentNullException("objectType");
            }
            if (!objectType.IsSubclassOf(typeof(CoCData)))
            {
                throw new ArgumentException("objectType is not a subclass of type CoCData.");
            }

            var rows       = table.Rows;
            var properties = objectType.GetProperties();
            var parentObj  = (object)null;
            var objList    = new List <object>();
            var id         = -1;

            for (int x = 0; x < rows.Count; x++)
            {
                var childObj = Activator.CreateInstance(objectType);
                for (int i = 0; i < properties.Length; i++) // set property value loop
                {
                    var property = properties[i];
                    if (property.DeclaringType != objectType)
                    {
                        property.SetMethod.Invoke(childObj, new object[] { id });
                        continue;
                    }

                    if (HasIgnoreAttribute(property))
                    {
                        continue; // ignore CsvIgnoreAttribute
                    }
                    var propertyName = GetPropertyAttributeName(property);
                    var value        = rows[x][propertyName];
                    var parameters   = new object[] { value };

                    if (parentObj != null && value == DBNull.Value) // get data from parent
                    {
                        parameters = new object[] { property.GetMethod.Invoke(parentObj, null) }
                    }
                    ;
                    else if (value == DBNull.Value)
                    {
                        continue; // keep default value
                    }
                    var isParent = property.Name == "Name" && value != DBNull.Value;
                    property.SetMethod.Invoke(childObj, parameters);

                    if (isParent)
                    {
                        id       += 1;
                        parentObj = childObj;
                    }
                }
                objList.Add(childObj);
            }
            return(objList.ToArray());
        }