Пример #1
0
    private static void SetData(FieldInfo _info, CsvBase _csv, string _data)
    {
        try
        {
            switch (_info.FieldType.Name)
            {
            case "Int32":

                if (string.IsNullOrEmpty(_data))
                {
                    _info.SetValue(_csv, 0);
                }
                else
                {
                    _info.SetValue(_csv, int.Parse(_data));
                }

                break;

            case "Int64":

                if (string.IsNullOrEmpty(_data))
                {
                    _info.SetValue(_csv, 0);
                }
                else
                {
                    _info.SetValue(_csv, long.Parse(_data));
                }

                break;

            case "String":

                _info.SetValue(_csv, FixStringChangeLine(_data));

                break;

            case "Boolean":

                _info.SetValue(_csv, _data == "1" ? true : false);

                break;

            case "Single":

                if (string.IsNullOrEmpty(_data))
                {
                    _info.SetValue(_csv, 0);
                }
                else
                {
                    _info.SetValue(_csv, float.Parse(_data));
                }

                break;

            case "Double":

                if (string.IsNullOrEmpty(_data))
                {
                    _info.SetValue(_csv, 0);
                }
                else
                {
                    _info.SetValue(_csv, double.Parse(_data));
                }

                break;

            case "Int16":

                if (string.IsNullOrEmpty(_data))
                {
                    _info.SetValue(_csv, 0);
                }
                else
                {
                    _info.SetValue(_csv, short.Parse(_data));
                }

                break;

            case "Int32[]":

                int[] intResult;

                if (string.IsNullOrEmpty(_data))
                {
                    intResult = new int[0];
                }
                else if (_data == "^")
                {
                    intResult = new int[1];
                }
                else
                {
                    string[] strArr = _data.Split('$');

                    intResult = new int[strArr.Length];

                    for (int i = 0; i < strArr.Length; i++)
                    {
                        intResult[i] = int.Parse(strArr[i]);
                    }
                }

                _info.SetValue(_csv, intResult);

                break;

            case "Int64[]":

                long[] longResult;

                if (string.IsNullOrEmpty(_data))
                {
                    longResult = new long[0];
                }
                else if (_data == "^")
                {
                    longResult = new long[1];
                }
                else
                {
                    string[] strArr = _data.Split('$');

                    longResult = new long[strArr.Length];

                    for (int i = 0; i < strArr.Length; i++)
                    {
                        longResult[i] = long.Parse(strArr[i]);
                    }
                }

                _info.SetValue(_csv, longResult);

                break;

            case "String[]":

                string[] stringResult;

                if (string.IsNullOrEmpty(_data))
                {
                    stringResult = new string[0];
                }
                else if (_data == "^")
                {
                    stringResult = new string[1];
                }
                else
                {
                    string[] tmpStr = _data.Split('$');

                    stringResult = new string[tmpStr.Length];

                    for (int i = 0; i < tmpStr.Length; i++)
                    {
                        stringResult[i] = FixStringChangeLine(tmpStr[i]);
                    }
                }

                _info.SetValue(_csv, stringResult);

                break;

            case "Boolean[]":

                bool[] boolResult;

                if (string.IsNullOrEmpty(_data))
                {
                    boolResult = new bool[0];
                }
                else if (_data == "^")
                {
                    boolResult = new bool[1];
                }
                else
                {
                    string[] strArr = _data.Split('$');

                    boolResult = new bool[strArr.Length];

                    for (int i = 0; i < strArr.Length; i++)
                    {
                        boolResult[i] = strArr[i] == "1" ? true : false;
                    }
                }

                _info.SetValue(_csv, boolResult);

                break;

            case "Single[]":

                float[] floatResult;

                if (string.IsNullOrEmpty(_data))
                {
                    floatResult = new float[0];
                }
                else if (_data == "^")
                {
                    floatResult = new float[1];
                }
                else
                {
                    string[] strArr = _data.Split('$');

                    floatResult = new float[strArr.Length];

                    for (int i = 0; i < strArr.Length; i++)
                    {
                        floatResult[i] = float.Parse(strArr[i]);
                    }
                }

                _info.SetValue(_csv, floatResult);

                break;

            case "Double[]":

                double[] doubleResult;

                if (string.IsNullOrEmpty(_data))
                {
                    doubleResult = new double[0];
                }
                else if (_data == "^")
                {
                    doubleResult = new double[1];
                }
                else
                {
                    string[] strArr = _data.Split('$');

                    doubleResult = new double[strArr.Length];

                    for (int i = 0; i < strArr.Length; i++)
                    {
                        doubleResult[i] = double.Parse(strArr[i]);
                    }
                }

                _info.SetValue(_csv, doubleResult);

                break;

            case "Int16[]":

                short[] shortResult;

                if (string.IsNullOrEmpty(_data))
                {
                    shortResult = new short[0];
                }
                else if (_data == "^")
                {
                    shortResult = new short[1];
                }
                else
                {
                    string[] strArr = _data.Split('$');

                    shortResult = new short[strArr.Length];

                    for (int i = 0; i < strArr.Length; i++)
                    {
                        shortResult[i] = short.Parse(strArr[i]);
                    }
                }

                _info.SetValue(_csv, shortResult);

                break;

            default:

                throw new Exception("csv表中的类型不支持反射  setData:" + _info.Name + "   " + _info.FieldType.Name + "   " + _data);
            }
        }
        catch (Exception e)
        {
            string str = "setData:" + _info.Name + "   " + _info.FieldType.Name + "   " + _data + "   " + _data.Length + Environment.NewLine;

            Console.WriteLine(str + "   " + e.ToString());
        }
    }
Пример #2
0
    private static void setData(FieldInfo _info, CsvBase _csv, string _data)
    {
        string str = "setData:" + _info.Name + "   " + _info.FieldType.Name + "   " + _data + "   " + _data.Length + Environment.NewLine;

        //Debug.Log(str);
        try
        {
            switch (_info.FieldType.Name)
            {
            case "Int32":

                if (string.IsNullOrEmpty(_data))
                {
                    _info.SetValue(_csv, 0);
                }
                else
                {
                    _info.SetValue(_csv, Int32.Parse(_data));
                }

                break;

            case "String":

                _info.SetValue(_csv, _data);

                break;

            case "Boolean":

                _info.SetValue(_csv, _data == "1" ? true : false);

                break;

            case "Single":

                if (string.IsNullOrEmpty(_data))
                {
                    _info.SetValue(_csv, 0);
                }
                else
                {
                    _info.SetValue(_csv, float.Parse(_data));
                }

                break;

            case "Int32[]":

                int[] intResult;

                if (!string.IsNullOrEmpty(_data))
                {
                    string[] strArr = _data.Split("$".ToCharArray());

                    intResult = new int[strArr.Length];

                    for (int i = 0; i < strArr.Length; i++)
                    {
                        intResult[i] = Int32.Parse(strArr[i]);
                    }
                }
                else
                {
                    intResult = new int[0];
                }

                _info.SetValue(_csv, intResult);

                break;

            case "String[]":

                string[] stringResult;

                if (!string.IsNullOrEmpty(_data))
                {
                    stringResult = _data.Split("$".ToCharArray());
                }
                else
                {
                    stringResult = new string[0];
                }

                _info.SetValue(_csv, stringResult);

                break;

            case "Boolean[]":

                bool[] boolResult;

                if (!string.IsNullOrEmpty(_data))
                {
                    string[] strArr = _data.Split("$".ToCharArray());

                    boolResult = new bool[strArr.Length];

                    for (int i = 0; i < strArr.Length; i++)
                    {
                        boolResult[i] = strArr[i] == "1" ? true : false;
                    }
                }
                else
                {
                    boolResult = new bool[0];
                }

                _info.SetValue(_csv, boolResult);

                break;

            default:

                float[] floatResult;

                if (!string.IsNullOrEmpty(_data))
                {
                    string[] strArr = _data.Split("$".ToCharArray());

                    floatResult = new float[strArr.Length];

                    for (int i = 0; i < strArr.Length; i++)
                    {
                        floatResult[i] = float.Parse(strArr[i]);
                    }
                }
                else
                {
                    floatResult = new float[0];
                }

                _info.SetValue(_csv, floatResult);

                break;
            }
        }
        catch (Exception e)
        {
            Debug.Log(str);
        }
    }
Пример #3
0
    private static void setData(FieldInfo _info, CsvBase _csv, string _data)
    {
        string str = "setData:" + _info.Name + "   " + _info.FieldType.Name + "   " + _data + "   " + _data.Length + Environment.NewLine;

        //SuperDebug.Log(str);
        try
        {
            switch (_info.FieldType.Name)
            {
            case "Int32":

                if (string.IsNullOrEmpty(_data))
                {
                    _info.SetValue(_csv, 0);
                }
                else
                {
                    _info.SetValue(_csv, Int32.Parse(_data));
                }

                break;

            case "String":

                _info.SetValue(_csv, _data);

                break;

            case "Boolean":

                _info.SetValue(_csv, _data == "1" ? true : false);

                break;

            case "Single":

                if (string.IsNullOrEmpty(_data))
                {
                    _info.SetValue(_csv, 0);
                }
                else
                {
                    _info.SetValue(_csv, float.Parse(_data));
                }

                break;

            case "Double":

                if (string.IsNullOrEmpty(_data))
                {
                    _info.SetValue(_csv, 0);
                }
                else
                {
                    _info.SetValue(_csv, double.Parse(_data));
                }

                break;

            case "Int16":

                if (string.IsNullOrEmpty(_data))
                {
                    _info.SetValue(_csv, 0);
                }
                else
                {
                    _info.SetValue(_csv, Int16.Parse(_data));
                }

                break;

            case "Int32[]":

                int[] intResult;

                if (!string.IsNullOrEmpty(_data))
                {
                    string[] strArr = _data.Split('$');

                    intResult = new int[strArr.Length];

                    for (int i = 0; i < strArr.Length; i++)
                    {
                        intResult[i] = Int32.Parse(strArr[i]);
                    }
                }
                else
                {
                    intResult = new int[0];
                }

                _info.SetValue(_csv, intResult);

                break;

            case "String[]":

                string[] stringResult;

                if (!string.IsNullOrEmpty(_data))
                {
                    stringResult = _data.Split('$');
                }
                else
                {
                    stringResult = new string[0];
                }

                _info.SetValue(_csv, stringResult);

                break;

            case "Boolean[]":

                bool[] boolResult;

                if (!string.IsNullOrEmpty(_data))
                {
                    string[] strArr = _data.Split('$');

                    boolResult = new bool[strArr.Length];

                    for (int i = 0; i < strArr.Length; i++)
                    {
                        boolResult[i] = strArr[i] == "1" ? true : false;
                    }
                }
                else
                {
                    boolResult = new bool[0];
                }

                _info.SetValue(_csv, boolResult);

                break;

            case "Double[]":

                double[] doubleResult;

                if (!string.IsNullOrEmpty(_data))
                {
                    string[] strArr = _data.Split('$');

                    doubleResult = new double[strArr.Length];

                    for (int i = 0; i < strArr.Length; i++)
                    {
                        doubleResult[i] = double.Parse(strArr[i]);
                    }
                }
                else
                {
                    doubleResult = new double[0];
                }

                _info.SetValue(_csv, doubleResult);

                break;

            case "Int16[]":

                short[] shortResult;

                if (!string.IsNullOrEmpty(_data))
                {
                    string[] strArr = _data.Split('$');

                    shortResult = new short[strArr.Length];

                    for (int i = 0; i < strArr.Length; i++)
                    {
                        shortResult[i] = Int16.Parse(strArr[i]);
                    }
                }
                else
                {
                    shortResult = new short[0];
                }

                _info.SetValue(_csv, shortResult);

                break;

            default:

                throw new Exception("csv表中的类型不支持反射  setData:" + _info.Name + "   " + _info.FieldType.Name + "   " + _data);
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(str + "   " + e.ToString());
        }
    }
Пример #4
0
    // 设置一个对象的 field 字段值
    // 目前 typeStr 如果有值,表示枚举
    // 如果没值表示使用反射从 obj 取到字段类型。
    private static void SetFieldValue(object obj, string fieldName, string typeStr, string cell)
    {
        int iSharp = fieldName.IndexOf('#');

        if (iSharp == 1)
        {
            CsvBase cb = (CsvBase)obj;

            char   symbol = fieldName[0];
            string field  = fieldName.Substring(iSharp + 1);

            if (symbol == 'i')
            {
                int v;
                if (string.IsNullOrEmpty(cell))
                {
                    v = 0;
                }
                else
                {
                    if (!int.TryParse(cell, out v))
                    {
                        throw new Exception("In " + parsingCsvName + ": unknown int: " + cell);
                    }
                }
                cb.setI(field, v);
            }
            else if (symbol == 'b')
            {
                cb.setB(field, (cell == "1" || cell == "是"));
            }
            else if (symbol == 's')
            {
                cb.setS(field, (string)cell);
            }
            else if (symbol == 'f')
            {
                cb.setF(field, (float)(string.IsNullOrEmpty(cell) ? 0f : float.Parse(cell)));
            }
            else if (symbol == 'e')
            {
                if (string.IsNullOrEmpty(typeStr))
                {
                    throw new Exception("In " + parsingCsvName + ": XXXXXXXXX");
                }
                int v = getEnumValue(typeStr, cell);
                cb.setI(field, v);
            }
            else if (symbol == 'a')
            {
                string[] elems = cell.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                int[]    arr   = new int[elems.Length];
                for (int i = 0; i < elems.Length; i++)
                {
                    arr[i] = int.Parse(elems[i]);
                }

                cb.setObj(field, arr);
            }
            // DateTime
            else if (symbol == 'd')
            {
                cb.setObj(field, ParseDateTime(cell));
            }
            else
            {
                throw new Exception("In " + parsingCsvName + ": Unknown any type: " + symbol);
            }
        }
        else
        {
            object v = null;
            if (string.IsNullOrEmpty(typeStr) || typeStr == "i")
            {
                if (string.IsNullOrEmpty(cell.Trim()))
                {
                    v = 0;
                }
                else
                {
                    int intV = 0;
                    if (!int.TryParse(cell, out intV))
                    {
                        throw new Exception("In " + parsingCsvName + ": unknown int: " + cell);
                    }
                    else
                    {
                        v = intV;
                    }
                }
            }
            else if (typeStr == "b")
            {
                v = (bool)(cell == "1" || cell == "是");
            }
            else if (typeStr == "s")
            {
                v = (string)cell;
            }
            else if (typeStr == "f")
            {
                v = (float)(string.IsNullOrEmpty(cell) ? 0f : float.Parse(cell));
            }
            else if (typeStr == "a")
            {
                string[] elems = cell.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                int[]    arr   = new int[elems.Length];
                for (int i = 0; i < elems.Length; i++)
                {
                    arr[i] = int.Parse(elems[i]);
                }

                v = arr;
            }
            // DateTime
            else if (typeStr == "d")
            {
                v = ParseDateTime(cell);
            }
            else
            {
                v = getEnumValue(typeStr, cell);
            }

            if (!SetValue(obj, fieldName, v))
            {
                throw new Exception("In " + parsingCsvName + ": field not found or error type: " + fieldName + ":" + (typeStr == null ? "i" : typeStr));
            }
        }
    }