コード例 #1
0
        public static void LoadObjectDefaults(IDefaultsHolder defaultsHolder)
        {
            Type t = defaultsHolder.GetType();

            if (BusinessObject.ClassXmlSerializationCache.ContainsKey(t))
            {
                XmlSerializationCache classCache = BusinessObject.ClassXmlSerializationCache[t];

                XElement defaultSettings = defaultsHolder.DefaultsXml.Root.Element(classCache.Attribute.XmlField);

                XmlSerializationCache[] cache = BusinessObject.PropertiesXmlSerializationCache[t];

                for (int i = 0; i < cache.Length; i++)
                {
                    XmlSerializationCache c = cache[i];

                    XmlSerializableAttribute propAttr = c.Attribute;
                    if (!propAttr.UseAttribute && defaultSettings.Element(propAttr.XmlField) != null ||
                        propAttr.UseAttribute && defaultSettings.Attribute(propAttr.XmlField) != null) //zeby nie nullowal juz ustawionych ustawien
                    {
                        BusinessObjectHelper.DeserializeSingleValue(defaultsHolder, c.Property, propAttr, defaultSettings);
                    }
                }
            }
        }
コード例 #2
0
ファイル: BusinessObject.cs プロジェクト: MakoLab/fractus
        private static void CacheClass(Type t)
        {
            if (!BusinessObject.IsClassCached.ContainsKey(t))
            {
                bool isObjectToCache = false;

                // get the class attributes
                object[] obj = t.GetCustomAttributes(typeof(XmlSerializableAttribute), true);

                if (obj != null && obj.Length == 1)
                {
                    XmlSerializableAttribute attr = (XmlSerializableAttribute)obj[0];
                    BusinessObject.ClassXmlSerializationCache.Add(t, new XmlSerializationCache()
                    {
                        Attribute = attr
                    });
                    isObjectToCache = true;
                }

                obj = t.GetCustomAttributes(typeof(DatabaseMappingAttribute), true);

                if (obj != null && obj.Length > 0)
                {
                    DatabaseMappingCache[] cache = new DatabaseMappingCache[obj.Length];

                    for (int i = 0; i < obj.Length; i++)
                    {
                        DatabaseMappingAttribute attr = (DatabaseMappingAttribute)obj[i];
                        cache[i] = new DatabaseMappingCache()
                        {
                            Attribute = attr
                        };
                    }

                    BusinessObject.ClassDatabaseMappingCache.Add(t, cache);
                    isObjectToCache = true;
                }

                if (isObjectToCache)
                {
                    //get properties XmlSerializableAttribiute
                    LinkedList <XmlSerializationCache> xmlSerializableCacheList = new LinkedList <XmlSerializationCache>();

                    foreach (PropertyInfo propertyInfo in t.GetProperties())
                    {
                        obj = propertyInfo.GetCustomAttributes(typeof(XmlSerializableAttribute), true);

                        if (obj != null && obj.Length == 1)
                        {
                            XmlSerializableAttribute attr  = (XmlSerializableAttribute)obj[0];
                            XmlSerializationCache    cache = new XmlSerializationCache()
                            {
                                Attribute = attr, Property = propertyInfo
                            };

                            if (attr.ProcessLast)
                            {
                                xmlSerializableCacheList.AddLast(cache);
                            }
                            else
                            {
                                xmlSerializableCacheList.AddFirst(cache);
                            }
                        }
                    }

                    XmlSerializationCache[] xmlCache = new XmlSerializationCache[xmlSerializableCacheList.Count];

                    int u = 0;

                    foreach (XmlSerializationCache c in xmlSerializableCacheList)
                    {
                        xmlCache[u++] = c;
                    }

                    BusinessObject.PropertiesXmlSerializationCache.Add(t, xmlCache);

                    //get properties ComparableAttribiute
                    List <ComparableCache> comparableCacheList = new List <ComparableCache>();

                    foreach (PropertyInfo propertyInfo in t.GetProperties())
                    {
                        obj = propertyInfo.GetCustomAttributes(typeof(ComparableAttribute), true);

                        if (obj != null && obj.Length == 1)
                        {
                            ComparableAttribute attr  = (ComparableAttribute)obj[0];
                            ComparableCache     cache = new ComparableCache()
                            {
                                Attribute = attr, Property = propertyInfo
                            };
                            comparableCacheList.Add(cache);
                        }
                    }

                    BusinessObject.PropertiesComparableCache.Add(t, comparableCacheList.ToArray());

                    //get properties DatabaseMappingCache
                    List <DatabaseMappingCache> databaseMappingCacheList = new List <DatabaseMappingCache>();

                    foreach (PropertyInfo propertyInfo in t.GetProperties())
                    {
                        obj = propertyInfo.GetCustomAttributes(typeof(DatabaseMappingAttribute), true);

                        if (obj != null && obj.Length > 0)
                        {
                            foreach (object objAttr in obj)
                            {
                                DatabaseMappingAttribute attr  = (DatabaseMappingAttribute)objAttr;
                                DatabaseMappingCache     cache = new DatabaseMappingCache()
                                {
                                    Attribute = attr, Property = propertyInfo
                                };
                                databaseMappingCacheList.Add(cache);
                            }
                        }
                    }

                    BusinessObject.PropertiesDatabaseMappingCache.Add(t, databaseMappingCacheList.ToArray());

                    BusinessObject.IsClassCached.Add(t, true);
                }
            }
        }
コード例 #3
0
        public static void DeserializeSingleValue(object target, PropertyInfo propertyInfo, XmlSerializableAttribute attribute, XElement boElement)
        {
            Type type = propertyInfo.PropertyType;

            if (!String.IsNullOrEmpty(attribute.EncapsulatingXmlField))
            {
                boElement = boElement.Element(attribute.EncapsulatingXmlField);
            }

            /*if (boElement == null ||
             *  (!attribute.UseAttribute && attribute.XmlField != null && boElement.Element(attribute.XmlField) == null) ||
             *  (attribute.UseAttribute && attribute.XmlField != null && boElement.Attribute(attribute.XmlField) == null))
             *  propertyInfo.SetValue(target, null, null);
             * else*/
            if (boElement != null && attribute.XmlField != null)
            {
                if (boElement.Element(attribute.XmlField) != null || boElement.Attribute(attribute.XmlField) != null)
                {
                    if (propertyInfo.PropertyType == typeof(Boolean))
                    {
                        if (BusinessObjectHelper.ReadValueFromXml(boElement, attribute.XmlField, attribute.UseAttribute) == "1")
                        {
                            propertyInfo.SetValue(target, true, null);
                        }
                        else
                        {
                            propertyInfo.SetValue(target, false, null);
                        }
                    }
                    else if (type == typeof(String))
                    {
                        propertyInfo.SetValue(target, BusinessObjectHelper.ReadValueFromXml(boElement, attribute.XmlField, attribute.UseAttribute), null);
                    }
                    else if (type == typeof(Decimal) || type == typeof(Decimal?))
                    {
                        propertyInfo.SetValue(target, Convert.ToDecimal(BusinessObjectHelper.ReadValueFromXml(boElement, attribute.XmlField, attribute.UseAttribute), CultureInfo.InvariantCulture), null);
                    }
                    else if (type == typeof(Int32) || type == typeof(Int32?))
                    {
                        propertyInfo.SetValue(target, Convert.ToInt32(BusinessObjectHelper.ReadValueFromXml(boElement, attribute.XmlField, attribute.UseAttribute), CultureInfo.InvariantCulture), null);
                    }
                    else if (type == typeof(Guid) || type == typeof(Guid?))
                    {
                        propertyInfo.SetValue(target, new Guid(BusinessObjectHelper.ReadValueFromXml(boElement, attribute.XmlField, attribute.UseAttribute)), null);
                    }
                    else if (type == typeof(DateTime) || type == typeof(DateTime?))
                    {
                        propertyInfo.SetValue(target, DateTime.Parse(BusinessObjectHelper.ReadValueFromXml(boElement, attribute.XmlField, attribute.UseAttribute), CultureInfo.InvariantCulture), null);
                    }
                    else if (type == typeof(XElement))
                    {
                        XElement element = BusinessObjectHelper.ReadXmlValueFromXml(boElement, attribute.XmlField, attribute.UseAttribute);

                        if (element.FirstNode is XElement && !attribute.UseAttribute)
                        {
                            propertyInfo.SetValue(target, new XElement((XElement)element.FirstNode), null);
                        }
                        else if (!attribute.UseAttribute)
                        {
                            propertyInfo.SetValue(target, new XElement(attribute.XmlField, element.Value), null);
                        }
                        else
                        {
                            propertyInfo.SetValue(target, element, null);
                        }
                    }
                    else if (typeof(IBusinessObject).IsAssignableFrom(type))
                    {
                        if (!String.IsNullOrEmpty(attribute.XmlField))
                        {
                            IBusinessObject obj = BusinessObjectHelper.CreateRelatedBusinessObjectFromXmlElement((XElement)boElement.Element(attribute.XmlField).FirstNode, attribute.RelatedObjectType);
                            propertyInfo.SetValue(target, obj, null);
                        }
                        else                         //check if the nested object has attribute indicating from which node to deserialize him
                        {
                            if (BusinessObject.ClassXmlSerializationCache.ContainsKey(type))
                            {
                                XmlSerializationCache cache = BusinessObject.ClassXmlSerializationCache[type];

                                IBusinessObject obj = (IBusinessObject)Activator.CreateInstance(type, target);                                 //constructor(parent)
                                obj.Deserialize((XElement)boElement.Element(cache.Attribute.XmlField));
                                propertyInfo.SetValue(target, obj, null);
                            }
                        }
                    }
                    else if (type.IsEnum)
                    {
                        propertyInfo.SetValue(target, Convert.ToInt32(BusinessObjectHelper.ReadValueFromXml(boElement, attribute.XmlField, attribute.UseAttribute), CultureInfo.InvariantCulture), null);
                    }
                    else if (typeof(ISerializableBusinessObjectContainer).IsAssignableFrom(type))
                    {
                        ((ISerializableBusinessObjectContainer)propertyInfo.GetValue(target, null)).Deserialize(boElement.Element(attribute.XmlField));
                    }
                    else
                    {
                        throw new InvalidOperationException("Unknown type to deserialize");
                    }
                }
            }
            else if (attribute.OverrideWithEmptyValue && type == typeof(Nullable))
            {
                ///DO przemyślenia
                //propertyInfo.SetValue(target, null, null);
            }
        }