/// <summary> /// Copies the values from all Billomat properties from obj to this /// </summary> /// <param name="obj">The object to create a copy of</param> protected void ApplyFrom(BillomatObject <T> obj) { PropertyInfo[] pi = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); foreach (PropertyInfo prop in pi) { var ba = (BillomatFieldAttribute)Attribute.GetCustomAttribute(prop, typeof(BillomatFieldAttribute)); if (ba == null) { continue; } try { object value = prop.GetValue(obj, null); prop.SetValue(this, value, null); } catch { } } }
/// <summary> /// Returns a dictionary containing all users: (user ID => user object) /// </summary> /// <returns></returns> public new static Dictionary <int, BillomatUser> GetList() { return(BillomatObject <BillomatUser> .GetList()); }
/// <summary> /// Returns a dictionary containing all templates: (template ID => template object) /// </summary> /// <returns></returns> public new static Dictionary <int, BillomatTemplate> GetList() { return(BillomatObject <BillomatTemplate> .GetList()); }
/// <summary> /// Creates an XML element and sets its child nodes according to the properties of the /// specified Billomat object. /// </summary> /// <param name="tagName">The tag name of the new XML element</param> /// <param name="obj">The Billomat object to be converted to XML</param> /// <exception cref="BillomatNet.BillomatException">Thrown if an unsupported property type is encountered</exception> /// <returns></returns> internal static XElement CreateXml(string tagName, BillomatObject <T> obj) { Type t = typeof(T); PropertyInfo[] properties = t.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); var root = new XElement(tagName); foreach (PropertyInfo prop in properties) { var ba = (BillomatFieldAttribute)Attribute.GetCustomAttribute(prop, typeof(BillomatFieldAttribute)); if (ba == null) { // only include properties with BillomatFieldAttribute continue; } var bro = (BillomatReadOnlyAttribute)Attribute.GetCustomAttribute(prop, typeof(BillomatReadOnlyAttribute)); if (bro != null) { // exclude properties with BillomatReadOnlyAttribute continue; } var elem = new XElement(ba.AttributeName); object value = prop.GetValue(obj, null); if (value == null) { continue; } if (prop.PropertyType == typeof(int)) { elem.Add(new XAttribute("type", "integer")); elem.Value = ((int)value).ToString(CultureInfo.InvariantCulture); } else if (prop.PropertyType == typeof(int?)) { elem.Add(new XAttribute("type", "integer")); elem.Value = ((int?)value).Value.ToString(CultureInfo.InvariantCulture); } else if (prop.PropertyType == typeof(float)) { elem.Add(new XAttribute("type", "float")); elem.Value = ((float)value).ToString(CultureInfo.InvariantCulture); } else if (prop.PropertyType == typeof(float?)) { elem.Add(new XAttribute("type", "float")); elem.Value = ((float?)value).Value.ToString(CultureInfo.InvariantCulture); } else if (prop.PropertyType == typeof(DateTime)) { elem.Add(new XAttribute("type", "datetime")); elem.Value = ((DateTime)value).ToString("s"); } else if (prop.PropertyType == typeof(DateTime?)) { elem.Add(new XAttribute("type", "datetime")); elem.Value = ((DateTime?)value).Value.ToString("s"); } else if (prop.PropertyType == typeof(string)) { elem.Value = (string)value; } else if (prop.PropertyType == typeof(bool)) { elem.Value = ((bool)value) ? "1" : "0"; } else { throw new BillomatException(string.Format("Property type '{0}' is not supported as Billomat field", prop.PropertyType.FullName), new NotSupportedException()); } root.Add(elem); } return(root); }