예제 #1
0
        public object FromXml(XElement element)
        {
            var proElements = element.Elements();

            if (proElements == null)
            {
                return(null);
            }
            string assembly = PropertyConverter.AttToString(element, "assembly");
            string type     = PropertyConverter.AttToString(element, "type");

            GeoDo.Core.XmlConstructorAttribute ctorAtt = GetCtorAttribute(assembly, type);
            object obj = null;

            if (ctorAtt == null)
            {
                obj = Activator.CreateInstance(assembly, type);
            }
            else
            {
                Assembly ass      = Assembly.Load(assembly);
                Type     t        = ass.GetType(type);
                object[] ctorArgs = GetCotorArgs(ctorAtt);
                obj = Activator.CreateInstance(t, ctorArgs);
            }
            if (obj is ObjectHandle)
            {
                obj = (obj as ObjectHandle).Unwrap();
            }
            GeoDo.Core.XmlContextVarAttribute xmlContextVarAtt = ContextVarAttribute(obj);
            if (xmlContextVarAtt != null)
            {
                Object2Xml.PersistContextEnv.Put(xmlContextVarAtt.VarName, obj);
            }
            foreach (XElement pElement in proElements)
            {
                if (ReadValueTypeProperty(pElement, obj))
                {
                    continue;
                }
                ReadComplexProperty(pElement, obj);
            }
            TryDoLoadAction(obj);
            return(obj);
        }
예제 #2
0
        private bool ReadValueTypeProperty(XElement pElement, object obj)
        {
            string proName = pElement.Name.LocalName;
            string type    = PropertyConverter.AttToString(pElement, "type");
            object value   = null;

            if (type == typeof(bool).ToString())
            {
                value = PropertyConverter.AttToBool(pElement, "value");
            }
            else if (type == typeof(int).ToString())
            {
                value = PropertyConverter.AttToInt(pElement, "value");
            }
            else if (type == typeof(string).ToString())
            {
                value = PropertyConverter.AttToString(pElement, "value");
            }
            else if (type == typeof(float).ToString())
            {
                value = PropertyConverter.AttToFloat(pElement, "value");
            }
            else if (type == typeof(byte).ToString())
            {
                value = PropertyConverter.AttToByte(pElement, "value");
            }
            else if (type == typeof(double).ToString())
            {
                value = PropertyConverter.AttToDouble(pElement, "value");
            }
            else
            {
                return(false);
            }
            if (obj.GetType().GetMethod("set_" + proName) != null)
            {
                obj.GetType().InvokeMember(proName, BindingFlags.SetProperty, null, obj, new object[] { value });
            }
            return(true);
        }