Esempio n. 1
0
        private void ReadComplexProperty(XElement pElement, object obj)
        {
            string       proName = pElement.Name.LocalName;
            PropertyInfo pInfo   = obj.GetType().GetProperty(proName);

            object[]            atts           = pInfo.GetCustomAttributes(typeof(XmlPersistAttribute), false);
            XmlPersistAttribute xmlPersisitAtt = atts[0] as XmlPersistAttribute;
            IPropertyConverter  convter        = Activator.CreateInstance(xmlPersisitAtt.PropertyConverter) as IPropertyConverter;
            object value = convter.FromXml(pElement);

            obj.GetType().InvokeMember(proName, BindingFlags.SetProperty, null, obj, new object[] { value });
        }
Esempio n. 2
0
        private void ReadComplexProperty(XElement pElement, object obj)
        {
            string       proName = pElement.Name.LocalName;
            PropertyInfo pInfo   = obj.GetType().GetProperty(proName);

            object[] atts = pInfo.GetCustomAttributes(typeof(GeoDo.Core.XmlPersistAttribute), true);
            if (atts == null || atts.Length == 0)
            {
                return;
            }
            GeoDo.Core.XmlPersistAttribute    xmlPersisitAtt   = atts[0] as GeoDo.Core.XmlPersistAttribute;
            GeoDo.Core.XmlContextVarAttribute xmlContextVarAtt = ContextVarAttribute(pInfo);
            object value = null;

            if (xmlPersisitAtt.PropertyConverter != null)
            {
                IPropertyConverter convter = Activator.CreateInstance(xmlPersisitAtt.PropertyConverter) as IPropertyConverter;
                convter.HostObject = obj;
                value = convter.FromXml(pElement);
            }
            else
            {
                var eles      = pElement.Elements();
                var elesArray = eles.ToArray();
                int count     = eles.Count();
                if (count > 0)
                {
                    if (pInfo.PropertyType.IsArray)
                    {
                        Array arry = Array.CreateInstance(xmlPersisitAtt.CollectionItemType, count);
                        for (int i = 0; i < count; i++)
                        {
                            arry.SetValue(FromXml(elesArray[i]), i);
                        }
                        value = arry;
                    }
                    else if (PropertyIsList(pInfo))
                    {
                        Type  type  = typeof(List <>);
                        Type  type1 = type.MakeGenericType(xmlPersisitAtt.CollectionItemType);
                        IList lst   = Activator.CreateInstance(type1) as IList;
                        for (int i = 0; i < count; i++)
                        {
                            lst.Add(FromXml(elesArray[i]));
                        }
                        value = lst;
                    }
                    else
                    {
                        value = FromXml(elesArray[0]);
                    }
                }
            }
            //
            if (obj.GetType().GetMethod("set_" + proName) != null)
            {
                obj.GetType().InvokeMember(proName, BindingFlags.SetProperty, null, obj, new object[] { value });
            }
            //
            if (xmlContextVarAtt != null)
            {
                Object2Xml.PersistContextEnv.Put(xmlContextVarAtt.VarName, value);
            }
        }