Ejemplo n.º 1
0
        private static void UpdateFieldValue <K, T>(ref T instance, ref K key, Dictionary <string, int> titleDic, List <string> strArr, string keyCSVMark)
        {
            FieldInfo[] fieldArr = instance.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance);
            for (int k = 0, fieldLen = fieldArr.Length; k < fieldLen; k++)
            {
                FieldInfo field = fieldArr[k];
                //Debug.Log(field.Name);
                System.Object[] attrArr = field.GetCustomAttributes(typeof(CSVElementAttribute), false);
                if (attrArr.Length > 0)
                {
                    CSVElementAttribute csvele = attrArr[0] as CSVElementAttribute;
                    if (!titleDic.ContainsKey(csvele.key))
                    {
                    }
                    string valueString = "";
                    try
                    {
                        valueString = strArr[titleDic[csvele.key]];
                    }
#pragma warning disable 168 // 声明了变量,但从未使用过
                    catch (Exception ex)
#pragma warning restore 168 // 声明了变量,但从未使用过
                    {
                        //Debug.Log("error key: " + csvele.key+" get value is wrong");
                    }

                    try
                    {
                        System.Object valueObject;
                        if (field.FieldType == typeof(sbyte))
                        {
                            valueObject = string.IsNullOrEmpty(valueString) ? default(sbyte) : sbyte.Parse(valueString);
                        }
                        else if (field.FieldType == typeof(byte))
                        {
                            valueObject = string.IsNullOrEmpty(valueString) ? default(byte) : byte.Parse(valueString);
                        }
                        else if (field.FieldType == typeof(short))
                        {
                            valueObject = string.IsNullOrEmpty(valueString) ? default(short) : short.Parse(valueString);
                        }
                        else if (field.FieldType == typeof(ushort))
                        {
                            valueObject = string.IsNullOrEmpty(valueString) ? default(ushort) : ushort.Parse(valueString);
                        }
                        else if (field.FieldType == typeof(int))
                        {
                            valueObject = string.IsNullOrEmpty(valueString) ? default(int) : int.Parse(valueString);
                        }
                        else if (field.FieldType == typeof(Int64))
                        {
                            valueObject = string.IsNullOrEmpty(valueString) ? default(Int64) : Int64.Parse(valueString);
                        }
                        else if (field.FieldType == typeof(uint))
                        {
                            valueObject = string.IsNullOrEmpty(valueString) ? default(uint) : uint.Parse(valueString);
                        }
                        else if (field.FieldType == typeof(long))
                        {
                            valueObject = string.IsNullOrEmpty(valueString) ? default(long) : long.Parse(valueString);
                        }
                        else if (field.FieldType == typeof(ulong))
                        {
                            valueObject = string.IsNullOrEmpty(valueString) ? default(ulong) : ulong.Parse(valueString);
                        }
                        else if (field.FieldType == typeof(float))
                        {
                            valueObject = string.IsNullOrEmpty(valueString) ? default(float) : float.Parse(valueString);
                        }
                        else if (field.FieldType.IsEnum)
                        {
                            valueObject = int.Parse(valueString);
                        }
                        else if (field.FieldType == typeof(bool))
                        {
                            valueObject = valueString != "0";
                        }
                        else if (field.FieldType == typeof(string))
                        {
                            valueObject = valueString;
                        }
                        else
                        {
                            throw new Exception("CSVUtil_ERROR: have not process type" + field.FieldType);
                        }
                        field.SetValue(instance, valueObject);

                        if (csvele.key == keyCSVMark)
                        {
                            key = (K)valueObject;
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message + "[[name]]" + field.Name + "[[value]]" + valueString + "[type]" + field.FieldType);
                    }
                }
            }

            PropertyInfo[] propertyInfoArr = instance.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
            for (int k = 0, propertyLen = propertyInfoArr.Length; k < propertyLen; k++)
            {
                PropertyInfo    pi      = propertyInfoArr[k];
                System.Object[] attrArr = pi.GetCustomAttributes(typeof(CSVElementAttribute), false);
                if (attrArr.Length > 0)
                {
                    CSVElementAttribute csvele = attrArr[0] as CSVElementAttribute;
                    if (!titleDic.ContainsKey(csvele.key))
                    {
                        Console.WriteLine(csvele.key);
                    }
                    string valueString = strArr[titleDic[csvele.key]];

                    try
                    {
                        System.Object valueObject;
                        if (pi.PropertyType == typeof(sbyte))
                        {
                            valueObject = string.IsNullOrEmpty(valueString) ? default(sbyte) : sbyte.Parse(valueString);
                        }
                        else if (pi.PropertyType == typeof(byte))
                        {
                            valueObject = string.IsNullOrEmpty(valueString) ? default(byte) : byte.Parse(valueString);
                        }
                        else if (pi.PropertyType == typeof(short))
                        {
                            valueObject = string.IsNullOrEmpty(valueString) ? default(short) : short.Parse(valueString);
                        }
                        else if (pi.PropertyType == typeof(ushort))
                        {
                            valueObject = string.IsNullOrEmpty(valueString) ? default(ushort) : ushort.Parse(valueString);
                        }
                        else if (pi.PropertyType == typeof(int))
                        {
                            valueObject = string.IsNullOrEmpty(valueString) ? default(int) : int.Parse(valueString);
                        }
                        else if (pi.PropertyType == typeof(Int64))
                        {
                            valueObject = string.IsNullOrEmpty(valueString) ? default(Int64) : Int64.Parse(valueString);
                        }
                        else if (pi.PropertyType == typeof(uint))
                        {
                            valueObject = string.IsNullOrEmpty(valueString) ? default(uint) : uint.Parse(valueString);
                        }
                        else if (pi.PropertyType == typeof(long))
                        {
                            valueObject = string.IsNullOrEmpty(valueString) ? default(long) : long.Parse(valueString);
                        }
                        else if (pi.PropertyType == typeof(ulong))
                        {
                            valueObject = string.IsNullOrEmpty(valueString) ? default(ulong) : ulong.Parse(valueString);
                        }
                        else if (pi.PropertyType == typeof(float))
                        {
                            valueObject = string.IsNullOrEmpty(valueString) ? default(float) : float.Parse(valueString);
                        }
                        else if (pi.PropertyType.IsEnum)
                        {
                            valueObject = int.Parse(valueString);
                        }
                        else if (pi.PropertyType == typeof(bool))
                        {
                            valueObject = valueString != "0";
                        }
                        else if (pi.PropertyType == typeof(string))
                        {
                            valueObject = valueString;
                        }
                        else
                        {
                            throw new Exception("CSVUtil_ERROR: have not process type" + pi.PropertyType);
                        }
                        pi.SetValue(instance, valueObject, null);

                        if (csvele.key == keyCSVMark)
                        {
                            key = (K)valueObject;
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message + "[[name]]" + pi.Name + "[[value]]" + valueString + "[type]" + pi.PropertyType);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public static Dictionary <K, T> ParseContent <K, T>(string content, string keyCSVMark) where T : new()
        {
            Dictionary <K, T> result = new Dictionary <K, T>();

            string[] lineArr = content.Split(SYMBOL_LINE, StringSplitOptions.RemoveEmptyEntries);
            Dictionary <string, int> titleDic = ParseTitle(lineArr[0]);

            for (int i = 1, lineLen = lineArr.Length; i < lineLen; i++)//第一行是title所以i=1开始
            {
                K             key    = default(K);
                List <string> strArr = ReaderLine(lineArr[i]);

                T           instance = System.Activator.CreateInstance <T>();
                FieldInfo[] fieldArr = instance.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance);
                for (int k = 0, fieldLen = fieldArr.Length; k < fieldLen; k++)
                {
                    FieldInfo field = fieldArr[k];
                    //Debug.Log(field.Name);
                    System.Object[] attrArr = field.GetCustomAttributes(typeof(CSVElementAttribute), false);
                    if (attrArr.Length > 0)
                    {
                        CSVElementAttribute csvele = attrArr[0] as CSVElementAttribute;
                        if (!titleDic.ContainsKey(csvele.key))
                        {
                        }
                        string valueString = "";
                        try
                        {
                            valueString = strArr[titleDic[csvele.key]];
                        }
#pragma warning disable 168 // 声明了变量,但从未使用过
                        catch (Exception ex)
#pragma warning restore 168 // 声明了变量,但从未使用过
                        {
                            //Debug.Log("error key: " + csvele.key+" get value is wrong");
                        }

                        try
                        {
                            System.Object valueObject;
                            if (field.FieldType == typeof(sbyte))
                            {
                                valueObject = string.IsNullOrEmpty(valueString) ? default(sbyte) : sbyte.Parse(valueString);
                            }
                            else if (field.FieldType == typeof(byte))
                            {
                                valueObject = string.IsNullOrEmpty(valueString) ? default(byte) : byte.Parse(valueString);
                            }
                            else if (field.FieldType == typeof(short))
                            {
                                valueObject = string.IsNullOrEmpty(valueString) ? default(short) : short.Parse(valueString);
                            }
                            else if (field.FieldType == typeof(ushort))
                            {
                                valueObject = string.IsNullOrEmpty(valueString) ? default(ushort) : ushort.Parse(valueString);
                            }
                            else if (field.FieldType == typeof(int))
                            {
                                valueObject = string.IsNullOrEmpty(valueString) ? default(int) : int.Parse(valueString);
                            }
                            else if (field.FieldType == typeof(Int64))
                            {
                                valueObject = string.IsNullOrEmpty(valueString) ? default(Int64) : Int64.Parse(valueString);
                            }
                            else if (field.FieldType == typeof(uint))
                            {
                                valueObject = string.IsNullOrEmpty(valueString) ? default(uint) : uint.Parse(valueString);
                            }
                            else if (field.FieldType == typeof(long))
                            {
                                valueObject = string.IsNullOrEmpty(valueString) ? default(long) : long.Parse(valueString);
                            }
                            else if (field.FieldType == typeof(ulong))
                            {
                                valueObject = string.IsNullOrEmpty(valueString) ? default(ulong) : ulong.Parse(valueString);
                            }
                            else if (field.FieldType == typeof(float))
                            {
                                valueObject = string.IsNullOrEmpty(valueString) ? default(float) : float.Parse(valueString);
                            }
                            else if (field.FieldType.IsEnum)
                            {
                                valueObject = int.Parse(valueString);
                            }
                            else if (field.FieldType == typeof(bool))
                            {
                                valueObject = valueString != "0";
                            }
                            else if (field.FieldType == typeof(string))
                            {
                                valueObject = valueString;
                            }
                            else
                            {
                                throw new Exception("CSVUtil_ERROR: have not process type" + field.FieldType);
                            }
                            field.SetValue(instance, valueObject);

                            if (csvele.key == keyCSVMark)
                            {
                                key = (K)valueObject;
                            }
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.Message + "[[name]]" + field.Name + "[[value]]" + valueString + "[type]" + field.FieldType);
                        }
                    }
                }

                PropertyInfo[] propertyInfoArr = instance.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
                for (int k = 0, propertyLen = propertyInfoArr.Length; k < propertyLen; k++)
                {
                    PropertyInfo    pi      = propertyInfoArr[k];
                    System.Object[] attrArr = pi.GetCustomAttributes(typeof(CSVElementAttribute), false);
                    if (attrArr.Length > 0)
                    {
                        CSVElementAttribute csvele = attrArr[0] as CSVElementAttribute;
                        if (!titleDic.ContainsKey(csvele.key))
                        {
                            Console.WriteLine(csvele.key);
                        }
                        string valueString = strArr[titleDic[csvele.key]];

                        try
                        {
                            System.Object valueObject;
                            if (pi.PropertyType == typeof(sbyte))
                            {
                                valueObject = string.IsNullOrEmpty(valueString) ? default(sbyte) : sbyte.Parse(valueString);
                            }
                            else if (pi.PropertyType == typeof(byte))
                            {
                                valueObject = string.IsNullOrEmpty(valueString) ? default(byte) : byte.Parse(valueString);
                            }
                            else if (pi.PropertyType == typeof(short))
                            {
                                valueObject = string.IsNullOrEmpty(valueString) ? default(short) : short.Parse(valueString);
                            }
                            else if (pi.PropertyType == typeof(ushort))
                            {
                                valueObject = string.IsNullOrEmpty(valueString) ? default(ushort) : ushort.Parse(valueString);
                            }
                            else if (pi.PropertyType == typeof(int))
                            {
                                valueObject = string.IsNullOrEmpty(valueString) ? default(int) : int.Parse(valueString);
                            }
                            else if (pi.PropertyType == typeof(Int64))
                            {
                                valueObject = string.IsNullOrEmpty(valueString) ? default(Int64) : Int64.Parse(valueString);
                            }
                            else if (pi.PropertyType == typeof(uint))
                            {
                                valueObject = string.IsNullOrEmpty(valueString) ? default(uint) : uint.Parse(valueString);
                            }
                            else if (pi.PropertyType == typeof(long))
                            {
                                valueObject = string.IsNullOrEmpty(valueString) ? default(long) : long.Parse(valueString);
                            }
                            else if (pi.PropertyType == typeof(ulong))
                            {
                                valueObject = string.IsNullOrEmpty(valueString) ? default(ulong) : ulong.Parse(valueString);
                            }
                            else if (pi.PropertyType == typeof(float))
                            {
                                valueObject = string.IsNullOrEmpty(valueString) ? default(float) : float.Parse(valueString);
                            }
                            else if (pi.PropertyType.IsEnum)
                            {
                                valueObject = int.Parse(valueString);
                            }
                            else if (pi.PropertyType == typeof(bool))
                            {
                                valueObject = valueString != "0";
                            }
                            else if (pi.PropertyType == typeof(string))
                            {
                                valueObject = valueString;
                            }
                            else
                            {
                                throw new Exception("CSVUtil_ERROR: have not process type" + pi.PropertyType);
                            }
                            pi.SetValue(instance, valueObject, null);

                            if (csvele.key == keyCSVMark)
                            {
                                key = (K)valueObject;
                            }
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.Message + "[[name]]" + pi.Name + "[[value]]" + valueString + "[type]" + pi.PropertyType);
                        }
                    }
                }
                if (result.ContainsKey(key))
                {
                    Console.WriteLine("CSVUtil_ERROR:duplicate col name:" + key);
                }
                result.Add(key, instance);
            }

            return(result);
        }