Exemplo n.º 1
0
 public object ToObject(string line, BaseCsvData csvData)
 {
     if (string.IsNullOrEmpty(line))
         throw new ArgumentNullException("line不能为Null或者Empty");
     string[] allCells = line.Split(',');
     // 获取csvData的所有成员或者属性
     Type t = csvData.GetType();
     FieldInfo[] allFields = t.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
     foreach (FieldInfo field in allFields)
     {
         // 获取索引
         ColumnIndex = GetOffset(field);
         // 取出对应在CSV中的值,offset是从1开始
         string cell = allCells[ColumnIndex - 1];
         Type fieldType = field.FieldType;
         object fieldValue = TranslatValueFromString(cell, fieldType);
         // 给成员赋值
         field.SetValue(csvData, fieldValue);
     }
     // 考虑到可能有安全类型,所以还要再检查一次属性
     PropertyInfo[] allProps = t.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
     foreach (PropertyInfo prop in allProps)
     {
         if (IsDeclaringByType(t, prop))
         {
             // 获取索引
             ColumnIndex = GetOffset(prop);
             // 取出对应在CSV中的值,offset是从1开始
             string cell = allCells[ColumnIndex - 1];
             // 转成对应的类型
             object propValue = TranslatValueFromString(cell, prop.PropertyType);
             // 给属性赋值,非索引器
             prop.SetValue(csvData, propValue, null);
         }
     }
     return csvData.PrimaryKey;
 }
Exemplo n.º 2
0
        public string ToString(BaseCsvData csvData)
        {
            if (csvData == null)
                throw new ArgumentNullException("csvData不能为null");
            Type t = csvData.GetType();
            FieldInfo[] allFields = t.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
            PropertyInfo[] allProps = t.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
            int cellsCount = allFields.Length + allProps.Length;
            if (cellsCount == 0) return string.Empty;

            string[] allCells = new string[cellsCount];
            foreach (FieldInfo field in allFields)
            {
                ColumnIndex = GetOffset(field);
                string cell = null;
                if (field.FieldType.IsArray)
                {
                    Array arr = field.GetValue(csvData) as Array;
                    if (arr == null) cell = string.Empty;
                    else cell = ArrayToString(arr);
                }
                else if (field.FieldType.IsAssignableFrom(typeof(UnityEngine.Vector3)))
                {
                    cell = VectorToString((UnityEngine.Vector3)field.GetValue(csvData));
                }
                else if (field.FieldType.IsAssignableFrom(typeof(bool)))
                {
                    cell = BoolToString((bool)field.GetValue(csvData));
                }
                else
                {
                    object fieldValue = field.GetValue(csvData);
                    cell = fieldValue == null ? string.Empty : fieldValue.ToString();
                }
                allCells[ColumnIndex - 1] = cell;
            }
            foreach (PropertyInfo prop in allProps)
            {
                if (IsDeclaringByType(t, prop))
                {
                    ColumnIndex = GetOffset(prop);
                    object propValue = prop.GetValue(csvData, null);
                    string cell = "";
                    if (propValue != null)
                    {
                        if (prop.PropertyType.IsArray)
                        {
                            Array arr = propValue as Array;
                            if (arr == null) cell = string.Empty;
                            else cell = ArrayToString(arr);
                        }
                        else if (prop.PropertyType.IsAssignableFrom(typeof(UnityEngine.Vector3)))
                        {
                            cell = VectorToString((UnityEngine.Vector3)propValue);
                        }
                        else if (prop.PropertyType.IsAssignableFrom(typeof(bool)))
                        {
                            cell = BoolToString((bool)propValue);
                        }
                        else
                        {
                            cell = propValue.ToString();
                        }
                    }
                    allCells[ColumnIndex - 1] = cell;
                }
            }

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < cellsCount; i++)
            {
                sb.Append(allCells[i]);
                if (i != (cellsCount - 1))
                {
                    sb.Append(",");
                }
            }
            return sb.ToString();
        }