Exemplo n.º 1
0
 /// <summary>
 /// Gets an identifier for an object
 /// </summary>
 /// <param name="o">EZObject- if the object already exists,  use its ide</param>
 /// <param name="valToCheck">Value to check.  If it is greater than 0,  then return it</param>
 /// <returns></returns>
 internal static int GetId(this IEzObject o, ref int valToCheck)
 {
     if (d.ContainsKey(o))
     {
         return(d[o]);
     }
     if (valToCheck > 0)
     {
         return(valToCheck);
     }
     return(d[o] = gid++);
 }
Exemplo n.º 2
0
 /// <summary>
 /// Gets an identifier for an object
 /// </summary>
 /// <param name="o">EZObject- if the object already exists,  use its ide</param>
 /// <param name="valToCheck">Value to check.  If it is greater than 0,  then return it</param>
 /// <returns></returns>
 internal static int SetRefId(this IEzObject o)
 {
     if (!RefObjectXref.ContainsKey(o._id))
     {
         RefObjectXref.Add(o._id, o);
     }
     if (!d.ContainsKey(o))
     {
         d.Add(o, o._id);
     }
     return(o._id);
 }
Exemplo n.º 3
0
        /// <summary>
        /// Returns an item as an XML Node
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="item">The item.</param>
        /// <param name="_doc">The document.</param>
        /// <param name="ElementName">Name of the element.</param>
        /// <param name="KeyAttributeValue">The key attribute value.</param>
        /// <returns></returns>
        internal static XmlNode AsXmlNode <T>(this T item, XmlDocument _doc, string ElementName, string KeyAttributeValue)
        {
            try
            {
                if (item == null)
                {
                    return(null);
                }
                if (item.GetType().Name.Equals("String"))
                {
                    XmlNode singleNode = _doc.CreateElement(ElementName);
                    singleNode.InnerText = item.ToSafeString();
                    return(singleNode);
                }
                XmlNode nod = _doc.CreateElement(XmlConvert.EncodeName(ElementName));
                if (!string.IsNullOrEmpty(KeyAttributeValue))
                {
                    ((XmlElement)nod).SetAttribute("key", KeyAttributeValue);
                }

                foreach (PropertyInfo pi in item.GetType().GetProperties())
                {
                    var isIndexProperty = (pi.GetIndexParameters().Length > 0);
                    if (!isIndexProperty) //ignore indexer properties
                    {
                        var asRefAttribute = (AsRef)pi.GetCustomAttribute(typeof(AsRef));
                        if (asRefAttribute != null)
                        {
                            try
                            {
                                var       _refVal = pi.GetValue(item, null);
                                IEzObject refVal  = _refVal as IEzObject;
                                var       idValue = refVal.GetType().GetProperty(asRefAttribute.ReferenceFieldName).GetValue(refVal, null);
                                if (idValue != null)
                                {
                                    XmlNode refnod = nod.OwnerDocument.CreateElement(pi.Name);
                                    ((XmlElement)refnod).SetAttribute("ref", idValue.ToSafeString());
                                    nod.AppendChild(refnod);
                                }
                            }
                            catch (Exception ex)
                            {
                                throw new Exception(string.Format("Error while getting reference value for {0}", asRefAttribute.ReferenceFieldName), ex);
                            }
                        }
                        else
                        {
                            var            val         = pi.GetValue(item, null);
                            IXmlRenderable valToRender = val as IXmlRenderable;
                            if (valToRender != null)
                            {
                                nod.AppendChild(((IXmlRenderableInternal)valToRender).AsXml(_doc));
                            }
                            else if (((pi.PropertyType.FullName.Contains("DateTime") || pi.PropertyType.Name.Equals("String") || pi.PropertyType.IsPrimitive)) && (val != null))
                            {
                                XmlNode newnod = nod.OwnerDocument.CreateElement(pi.Name);
                                newnod.InnerText = val.ToSafeString();
                                nod.AppendChild(newnod);
                            }
                            else
                            {
                            }
                        }
                    }
                }
                return(nod);
            }
            catch (Exception)
            {
                throw;
            }
        }