예제 #1
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);
        }
예제 #2
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);
                    }
                }
            }
        }