Exemplo n.º 1
0
        private void SetPropertyValue(object obj, XmlNode propertyNode)
        {
            MemberInfox property = GetPropertyInfo(propertyNode, obj);

            if (property == null)
            {
                return;
            }
            property.SetValue(propertyNode.InnerText.Trim().ConvertTo(property.ValueType));
        }
Exemplo n.º 2
0
        public static int GetSizeConst(MemberInfox property)
        {
            var att = property.GetCustomAttributes(typeof(MarshalAsAttribute)) as MarshalAsAttribute;

            if (att == null)
            {
                return(-1);
            }
            return(att.SizeConst);
        }
Exemplo n.º 3
0
        /// Convert this SourceList to DataTable instance.

        public static System.Data.DataTable ToDataTable(this System.Collections.IList SourceList, string TableName)
        {
            System.Data.DataTable table = new System.Data.DataTable(TableName);
            if (SourceList.Count > 0)
            {
                foreach (var item in MemberInfox.GetMemberInfox(SourceList[0]))
                {
                    table.Columns.Add(item.Name, item.MemberType);
                }
                table.Populate(SourceList);
            }
            return(table);
        }
Exemplo n.º 4
0
        public static MemberInfox GetPropertyInfox(object obj, string PropertyPath, bool SearchForPriviteFieldInCasePublicFieldIsReadOnly = true)
        {
            MemberInfox info = null;

            foreach (var partName in (PropertyPath + "").Split('.'))//For nested properties like a.Name
            {
                info = new MemberInfox(obj, partName, SearchForPriviteFieldInCasePublicFieldIsReadOnly);
                if (!info.MemberExists)
                {
                    return(info);
                }
                obj = info.GetValue();
            }
            return(info);
        }
Exemplo n.º 5
0
 public void DeserializeNode(XmlNode node, object Instance, bool ThrowOnError = false)
 {
     mapXPathNodeToObject[Utility.GetXPath(node)] = Instance;
     foreach (XmlNode propertyNode in node.SelectNodes("Property"))
     {
         try
         {
             MemberInfox propertyInfo  = GetPropertyInfo(propertyNode, Instance);
             object      PropertyValue = propertyInfo.GetValue();
             var         childs        = GetChild(propertyNode);
             object      refObj        = CheckIfReferenceNode(propertyNode);
             if (refObj != null)
             {
                 propertyInfo.SetValue(refObj);
             }
             else if (Utility.IsSimpleType(propertyInfo.ValueType))//Simple property
             {
                 SetPropertyValue(Instance, propertyNode);
             }
             else if (childs.Count > 0 && childs[0].Name == "Array")//Array found
             {
                 propertyInfo.SetValue(DeserializeIEnumerable(childs[0], propertyInfo.ValueType, null, ThrowOnError));
             }
             else//Complex types
             {
                 if (PropertyValue == null)
                 {
                     if (!Utility.TryCreateInstance(propertyInfo.ValueType, out PropertyValue))
                     {
                         continue;
                     }
                     propertyInfo.SetValue(PropertyValue);
                 }
                 DeserializeNode(propertyNode, PropertyValue, ThrowOnError);
             }
         }
         catch { if (ThrowOnError)
                 {
                     throw;
                 }
         }
     }
 }
Exemplo n.º 6
0
 private bool IsEqualDefaut(MemberInfox property)
 {
     try
     {
         if (!Utility.IsSimpleType(property.ValueType))
         {
             return(false);
         }
         var    val = property.GetValue();
         object att = property.GetCustomAttributes(typeof(DefaultValueAttribute));
         if (att != null)
         {
             return(object.Equals(val, (att as DefaultValueAttribute).Value));
         }
         return(object.Equals(val, Utility.GetDefault(property.MemberType)));
     }
     catch {  }
     return(true);
 }
Exemplo n.º 7
0
        private void SerializeProperty(object propertyValue, XmlNode node)
        {
            if (propertyValue == null)
            {
                return;
            }
            Type propertyType = propertyValue.GetType();

            if (Utility.IsSimpleType(propertyType))
            {
                node.InnerText = propertyValue.ConvertTo <string>();
            }
            else
            {
                if (MapObjectToNode.ContainsKey(propertyValue))//Prevent looping exceptions, when the instance refer to its self
                {
                    node.Attributes.Append(xmlDoc.CreateAttribute("ReferenceNode")).Value = Utility.GetXPath(MapObjectToNode[propertyValue]);
                    return;
                }
                else
                {
                    MapObjectToNode[propertyValue] = node;
                }
                if (Utility.IsIEnumerable(propertyType))
                {
                    var        elmentType = Utility.GetElementType(propertyValue.GetType());
                    XmlElement arrayNode  = (XmlElement)node.AppendChild(xmlDoc.CreateElement("Array"));

                    foreach (var item in Utility.GetIEnumerableItems(propertyValue))
                    {
                        var elementNode = arrayNode.AppendChild(xmlDoc.CreateElement("Item"));
                        if (item != null && elmentType.GetUnderlyingType() != item.GetType().GetUnderlyingType())
                        {
                            elementNode.Attributes.Append(xmlDoc.CreateAttribute("ElementDataType")).Value = Utility.GetTypeFullName(item.GetType(), elmentType);
                        }
                        SerializeProperty(item, elementNode);
                    }
                    ExtractSharedValues(arrayNode);
                }
                else//Complex types, such as OlvFormSpec.Button
                {
                    int childs = node.ChildNodes.Count;
                    foreach (var propx in MemberInfox.GetMemberInfox(propertyValue))
                    {
                        var att = propx.GetCustomAttributes(typeof(ObjectSerializerAttribute)) as ObjectSerializerAttribute;
                        if (att != null && att.DontSerialize)
                        {
                            continue;
                        }
                        if (IsEqualDefaut(propx))
                        {
                            continue;
                        }
                        var propertyNode = node.AppendChild(xmlDoc.CreateElement("Property"));
                        propertyNode.Attributes.Append(xmlDoc.CreateAttribute("PropertyName")).Value = propx.Name;
                        if (propx.MemberType.GetUnderlyingType() != propx.ValueType.GetUnderlyingType())
                        {
                            propertyNode.Attributes.Append(xmlDoc.CreateAttribute("PropertyDataType")).Value = Utility.GetTypeFullName(propx.ValueType, propx.MemberType);
                        }
                        SerializeProperty(propx.GetValue(), propertyNode);
                        if (propertyNode.ChildNodes.Count == 0 && propertyNode.Attributes.Count < 2)//No need for complex object without having inner nodes
                        {
                            node.RemoveChild(propertyNode);
                        }
                    }
                    if (node.ChildNodes.Count == childs)
                    {
                        MapObjectToNode.Remove(propertyValue);
                    }
                }
            }
        }