Exemplo n.º 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 "String":

                _info.SetValue(_csv, PublicTools.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))
                {
                    string[] strArr = _data.Split('$');

                    intResult = new int[strArr.Length];

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

                _info.SetValue(_csv, intResult);

                break;

            case "String[]":

                string[] stringResult;

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

                    stringResult = new string[tmpStr.Length];

                    for (int i = 0; i < tmpStr.Length; i++)
                    {
                        stringResult[i] = PublicTools.FixStringChangeLine(tmpStr[i]);
                    }
                }
                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 "Single[]":

                float[] floatResult;

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

                    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;

            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] = short.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)
        {
            string str = "setData:" + _info.Name + "   " + _info.FieldType.Name + "   " + _data + "   " + _data.Length + Environment.NewLine;

            Console.WriteLine(str + "   " + e.ToString());
        }
    }