コード例 #1
0
        protected static List <T> ToObjectList <T>(XmlNode sectionNode, string xpathOfNodes)
            where T : new()
        {
            if (sectionNode == null)
            {
                return(null);
            }
            Type         type        = typeof(T);
            bool         isStringMap = (type == typeof(Dictionary <string, string>));
            SidConverter converter   = new SidConverter();
            List <T>     objList     = new List <T>();
            XmlNodeList  nodes       = sectionNode.SelectNodes(xpathOfNodes);

            if (nodes == null)
            {
                return(null);
            }
            //XmlSerializer serializer = new XmlSerializer(type);
            foreach (XmlNode node in nodes)
            {
                //XmlReader xmlReader = new XmlNodeReader(node);
                //T obj = (T)serializer.Deserialize(xmlReader);

                dynamic obj = new T();
                foreach (XmlAttribute attri in node.Attributes)
                {
                    if (isStringMap)
                    {
                        obj[attri.Name] = attri.Value.ToString();
                        continue;
                    }
                    PropertyInfo propertyInfo  = type.GetProperty(attri.Name);
                    object       propertyValue = (propertyInfo == null)?attri.Value.ToString():converter.Xml2Obj(attri.Value.ToString(), propertyInfo.PropertyType);
                    if (propertyInfo != null)
                    {
                        propertyInfo.SetValue(obj, propertyValue, null);
                    }

                    IConfig cfg = obj as IConfig;
                    if (cfg != null)
                    {
                        cfg.AssignValue(attri.Name, propertyValue);
                    }
                }

                objList.Add(obj);
            }
            return(objList);
        }