Beispiel #1
0
    /// 字段复制
    private void SetFieldValue(FieldInfo field, object obj, Dictionary <string, string> hash)
    {
        if (obj == null)
        {
            return;
        }
        if (field == null)
        {
            return;
        }
        if (hash == null)
        {
            return;
        }

        string fieldName = field.Name;

        if (!string.IsNullOrEmpty(fieldNameEndStr) && hash.ContainsKey(fieldName + fieldNameEndStr))
        {
            fieldName = fieldName + fieldNameEndStr;
        }

        if (!hash.ContainsKey(fieldName))
        {
            return;
        }

        try
        {
            if (field.FieldType == typeof(string[][]))
            {
                if (hash[fieldName] == null || hash[fieldName] == "" || hash[fieldName] == "#")
                {
                    field.SetValue(obj, null);
                    return;
                }
                //先看是不是用#作分割
                string[] arrSplit1 = hash[fieldName].Split("#".ToCharArray());
                if (null != arrSplit1)
                {
                    List <string[]> lstSplit2 = new List <string[]>();
                    for (int i = 0; i < arrSplit1.Length; i++)
                    {
                        //空的就不做考虑
                        if (string.IsNullOrEmpty(arrSplit1[i]))
                        {
                            continue;
                        }
                        //再分
                        string[] arrTemp = arrSplit1[i].Split("|".ToCharArray());
                        lstSplit2.Add(arrTemp);
                    }
                    if (lstSplit2.Count > 0)
                    {
                        //二维数组赋给二维数组
                        field.SetValue(obj, lstSplit2.ToArray());
                    }
                }
                return;
            }
            /// long[][]
            if (field.FieldType == typeof(long[][]))
            {
                if (hash[fieldName] == "#" || string.IsNullOrEmpty(hash[fieldName]))
                {
                    field.SetValue(obj, new long[0][]);
                }
                else
                {
                    //先看是不是用#作分割
                    string[] arrSplit1 = hash[fieldName].Split("#".ToCharArray());
                    if (null != arrSplit1)
                    {
                        List <long[]> lstSplit2 = new List <long[]>();
                        for (int i = 0; i < arrSplit1.Length; i++)
                        {
                            //空的就不做考虑
                            if (string.IsNullOrEmpty(arrSplit1[i]))
                            {
                                continue;
                            }
                            //再分
                            string[] arrTemp = arrSplit1[i].Split("|".ToCharArray());
                            long[]   arrLong = new long[arrTemp.Length];
                            for (int j = 0; j < arrTemp.Length; j++)
                            {
                                long.TryParse(arrTemp[j], out arrLong[j]);
                            }
                            lstSplit2.Add(arrLong);
                        }
                        if (lstSplit2.Count > 0)
                        {
                            //二维数组赋给二维数组
                            field.SetValue(obj, lstSplit2.ToArray());
                        }
                    }
                }
                return;
            }
            /// float[][]
            if (field.FieldType == typeof(float[][]))
            {
                if (hash[fieldName] == "#" || string.IsNullOrEmpty(hash[fieldName]))
                {
                    field.SetValue(obj, new float[0][]);
                }
                else
                {
                    //先看是不是用#作分割
                    string[] arrSplit1 = hash[fieldName].Split("#".ToCharArray());
                    if (null != arrSplit1)
                    {
                        List <float[]> lstSplit2 = new List <float[]>();
                        for (int i = 0; i < arrSplit1.Length; i++)
                        {
                            //空的就不做考虑
                            if (string.IsNullOrEmpty(arrSplit1[i]))
                            {
                                continue;
                            }
                            //再分
                            string[] arrTemp = arrSplit1[i].Split("|".ToCharArray());
                            float[]  arrLong = new float[arrTemp.Length];
                            for (int j = 0; j < arrTemp.Length; j++)
                            {
                                float.TryParse(arrTemp[j], out arrLong[j]);
                            }
                            lstSplit2.Add(arrLong);
                        }
                        if (lstSplit2.Count > 0)
                        {
                            //二维数组赋给二维数组
                            field.SetValue(obj, lstSplit2.ToArray());
                        }
                    }
                }
                return;
            }
            /// long[] 获得数组
            if (field.FieldType == typeof(long[]))
            {
                if (hash[fieldName] == "#" || string.IsNullOrEmpty(hash[fieldName]))
                {
                    field.SetValue(obj, new long[0]);
                }
                else
                {
                    string[] valS = hash[fieldName].Split("|".ToCharArray());
                    long[]   valL = new long[valS.Length];
                    for (int i = 0; i < valS.Length; i++)
                    {
                        if (!string.IsNullOrEmpty(valS[i]))
                        {
                            valL[i] = long.Parse(valS[i]);
                        }
                    }
                    field.SetValue(obj, valL);
                }
                return;
            }
            /// float[] 获得数组
            if (field.FieldType == typeof(float[]))
            {
                if (hash[fieldName] == "#" || string.IsNullOrEmpty(hash[fieldName]))
                {
                    field.SetValue(obj, new float[0]);
                }
                else
                {
                    string[] valS = hash[fieldName].Split("|".ToCharArray());
                    float[]  valL = new float[valS.Length];
                    for (int i = 0; i < valS.Length; i++)
                    {
                        float.TryParse(valS[i], out valL[i]);
                    }
                    field.SetValue(obj, valL);
                }
                return;
            }

            //如果不是二维数组,直接存起来
            if (field.FieldType == typeof(string[]))
            {
                if (hash[fieldName] == null || hash[fieldName] == "" || hash[fieldName] == "#")
                {
                    field.SetValue(obj, null);
                }
                else
                {
                    field.SetValue(obj, hash[fieldName].Split("|".ToCharArray()));
                }
                return;
            }

            if (field.FieldType == typeof(bool))
            {
                field.SetValue(obj, hash[fieldName] == "1");
                return;
            }
            if (field.FieldType == typeof(long))
            {
                if (hash[fieldName] == "#" || string.IsNullOrEmpty(hash[fieldName]))
                {
                    field.SetValue(obj, (long)0);
                }
                else
                {
                    field.SetValue(obj, long.Parse(hash[fieldName]));
                }
                return;
            }
            if (field.FieldType == typeof(ulong))
            {
                if (hash[fieldName] == "#" || string.IsNullOrEmpty(hash[fieldName]))
                {
                    field.SetValue(obj, (ulong)0);
                }
                else
                {
                    field.SetValue(obj, ulong.Parse(hash[fieldName]));
                }
                return;
            }
            if (field.FieldType == typeof(int))
            {
                if (hash[fieldName] == "#" || string.IsNullOrEmpty(hash[fieldName]))
                {
                    field.SetValue(obj, 0);
                }
                else
                {
                    field.SetValue(obj, int.Parse(hash[fieldName]));
                }
                return;
            }
            if (field.FieldType == typeof(float))
            {
                if (hash[fieldName] == "#" || string.IsNullOrEmpty(hash[fieldName]))
                {
                    field.SetValue(obj, 0f);
                }
                else
                {
                    field.SetValue(obj, float.Parse(hash[fieldName]));
                }
                return;
            }
            /// 优化内存
            field.SetValue(obj, string.Intern(hash[fieldName]));             //.Replace("\\n", "\n"
        } catch {
            //Debug.LogError(se.ToString());
            if (hash.ContainsKey("ID"))
            {
                UtilLog.LogWarning(string.Format("UtilCsvReader Set Value Error : {0}.{1} = {2}, ID = {3}", obj.GetType().Name, fieldName, hash[fieldName], hash["ID"]));
            }
            else
            {
                UtilLog.LogWarning(string.Format("UtilCsvReader Set Value Error : {0}.{1} = {2}", obj.GetType().Name, fieldName, hash[fieldName]));
            }
        }
    }
Beispiel #2
0
    /// 字段复制
    private void SetFieldValue <T>(System.Reflection.FieldInfo sField, T sObj)
        where T : new()
    {
        if (sObj == null)
        {
            return;
        }
        if (sField == null)
        {
            return;
        }
        /// 数据中没有就返回
        string fieldName = sField.Name;

        if (!_data.ContainsKey(fieldName))
        {
            return;
        }

        try
        {
            if (sField.FieldType == typeof(string[][]))
            {
                if (_data[fieldName] == null || _data[fieldName] == "" || _data[fieldName] == "#")
                {
                    sField.SetValue(sObj, null);
                    return;
                }
                //先看是不是用#作分割
                string[] arrSplit1 = _data[fieldName].Split("#".ToCharArray());
                if (null != arrSplit1)
                {
                    List <string[]> lstSplit2 = new List <string[]>();
                    for (int i = 0; i < arrSplit1.Length; i++)
                    {
                        //空的就不做考虑
                        if (string.IsNullOrEmpty(arrSplit1[i]))
                        {
                            continue;
                        }
                        //再分
                        string[] arrTemp = arrSplit1[i].Split("|".ToCharArray());
                        lstSplit2.Add(arrTemp);
                    }
                    if (lstSplit2.Count > 0)
                    {
                        //二维数组赋给二维数组
                        sField.SetValue(sObj, lstSplit2.ToArray());
                    }
                }
                return;
            }

            /// 2016.12.16 add by gus 获得数组
            if (sField.FieldType == typeof(long[]))
            {
                if (_data[fieldName] == "#" || string.IsNullOrEmpty(_data[fieldName]))
                {
                    sField.SetValue(sObj, new long[0]);
                }
                else
                {
                    string[] valS = _data[fieldName].Split("|".ToCharArray());
                    long[]   valL = new long[valS.Length];
                    for (int i = 0; i < valS.Length; i++)
                    {
                        if (!string.IsNullOrEmpty(valS[i]))
                        {
                            valL[i] = long.Parse(valS[i]);
                        }
                    }
                    sField.SetValue(sObj, valL);
                }
                return;
            }
            //如果不是二维数组,直接存起来
            if (sField.FieldType == typeof(string[]))
            {
                if (_data[fieldName] == null || _data[fieldName] == "" || _data[fieldName] == "#")
                {
                    sField.SetValue(sObj, null);
                }
                else
                {
                    sField.SetValue(sObj, _data[fieldName].Split("|".ToCharArray()));
                }
                return;
            }

            if (sField.FieldType == typeof(bool))
            {
                sField.SetValue(sObj, _data[fieldName] == "1");
                return;
            }
            if (sField.FieldType == typeof(long))
            {
                if (_data[fieldName] == "#" || string.IsNullOrEmpty(_data[fieldName]))
                {
                    sField.SetValue(sObj, (long)0);
                }
                else
                {
                    sField.SetValue(sObj, long.Parse(_data[fieldName]));
                }
                return;
            }
            if (sField.FieldType == typeof(ulong))
            {
                if (_data[fieldName] == "#" || string.IsNullOrEmpty(_data[fieldName]))
                {
                    sField.SetValue(sObj, (ulong)0);
                }
                else
                {
                    sField.SetValue(sObj, ulong.Parse(_data[fieldName]));
                }
                return;
            }
            if (sField.FieldType == typeof(int))
            {
                if (_data[fieldName] == "#" || string.IsNullOrEmpty(_data[fieldName]))
                {
                    sField.SetValue(sObj, 0);
                }
                else
                {
                    sField.SetValue(sObj, int.Parse(_data[fieldName]));
                }
                return;
            }
            if (sField.FieldType == typeof(float))
            {
                if (_data[fieldName] == "#" || string.IsNullOrEmpty(_data[fieldName]))
                {
                    sField.SetValue(sObj, 0f);
                }
                else
                {
                    sField.SetValue(sObj, float.Parse(_data[fieldName]));
                }
                return;
            }
            /// 优化内存
            sField.SetValue(sObj, string.Intern(_data[fieldName]));
        }
        catch
        {
            UtilLog.LogWarning(string.Format("UtilInfoReader Set Value Error : {0}.{1} = {2}", sObj.GetType().Name, fieldName, _data[fieldName]));
        }
    }