コード例 #1
0
        private static List <T> LoadXMLText <T>(string text)
        {
            Exception exception;
            List <T>  list = new List <T>();

            try
            {
                if (string.IsNullOrEmpty(text))
                {
                    return(list);
                }
                Type type = typeof(T);
                Dictionary <int, Dictionary <string, string> > dictionary = XMLParser.LoadIntMap(XMLParser.LoadXML(text), text);
                PropertyInfo[] properties = type.GetProperties(~BindingFlags.Static);
                foreach (KeyValuePair <int, Dictionary <string, string> > pair in dictionary)
                {
                    object obj2 = type.GetConstructor(Type.EmptyTypes).Invoke(null);
                    foreach (PropertyInfo info in properties)
                    {
                        if (info.Name == "id")
                        {
                            info.SetValue(obj2, pair.Key, null);
                        }
                        else
                        {
                            try
                            {
                                if (pair.Value.ContainsKey(info.Name))
                                {
                                    object obj3 = Utils.GetValue(pair.Value[info.Name], info.PropertyType);
                                    info.SetValue(obj2, obj3, null);
                                }
                            }
                            catch (Exception exception1)
                            {
                                exception = exception1;
                                LoggerHelper.Debug(string.Concat(new object[] { "LoadXML error: ", pair.Value[info.Name], " ", info.PropertyType }), true, 0);
                                LoggerHelper.Except(exception, null);
                            }
                        }
                    }
                    list.Add((T)obj2);
                }
            }
            catch (Exception exception2)
            {
                exception = exception2;
                LoggerHelper.Except(exception, null);
                LoggerHelper.Error("error text: \n" + text, true);
            }
            return(list);
        }
コード例 #2
0
        private static List <T> LoadXMLText <T>(string text)
        {
            List <T> list = new List <T>();

            try
            {
                if (String.IsNullOrEmpty(text))
                {
                    return(list);
                }
                Type type = typeof(T);
                var  xml  = XMLParser.LoadXML(text);
                Dictionary <Int32, Dictionary <String, String> > map = XMLParser.LoadIntMap(xml, text);
                var props = type.GetProperties(~System.Reflection.BindingFlags.Static);
                foreach (var item in map)
                {
                    var obj = type.GetConstructor(Type.EmptyTypes).Invoke(null);
                    foreach (var prop in props)
                    {
                        if (prop.Name == "id")
                        {
                            prop.SetValue(obj, item.Key, null);
                        }
                        else
                        {
                            try
                            {
                                if (item.Value.ContainsKey(prop.Name))
                                {
                                    var value = Utils.GetValue(item.Value[prop.Name], prop.PropertyType);
                                    prop.SetValue(obj, value, null);
                                }
                            }
                            catch (Exception ex)
                            {
                                //LoggerHelper.Debug("LoadXML error: " + item.Value[prop.Name] + " " + prop.PropertyType);
                                LoggerHelper.Except(ex);
                            }
                        }
                    }
                    list.Add((T)obj);
                }
            }
            catch (Exception ex)
            {
                LoggerHelper.Except(ex);
                //LoggerHelper.Error("error text: \n" + text);
            }
            return(list);
        }