public string GetReferenceValueByPrimaryKey(Type referenceType, object primaryKey, object dataSource, string defaultPropertyName = null) { BeanDefinition definition = BeanDescriptor.GetDefinition(referenceType); BeanPropertyDescriptor primaryKeyProperty = definition.PrimaryKey; BeanPropertyDescriptor valueProperty = string.IsNullOrEmpty(defaultPropertyName) ? definition.DefaultProperty : definition.Properties[defaultPropertyName]; ICollection dataSourceColl = (ICollection)dataSource; if (dataSourceColl == null) { throw new NotSupportedException("DataSource must be an ICollection."); } object candidate = null; foreach (object item in dataSourceColl) { if (primaryKeyProperty.GetValue(item).Equals(primaryKey)) { candidate = item; break; } } if (candidate == null) { throw new NotSupportedException("The datasource does not contain an object with this primary key."); } return(valueProperty.ConvertToString(valueProperty.GetValue(candidate))); }
public void ConvertToStringFormatteur() { BeanDefinition beanDefinition = BeanDescriptor.GetDefinition(new Bean()); BeanPropertyDescriptor propertyDate = beanDefinition.Properties["Date"]; propertyDate.ConvertToString(DateTime.Now); }
/// <summary> /// Retourne la valeur d'une liste de référence à partir /// de son identifiant. /// </summary> /// <param name="referenceType">Type de la liste de référence.</param> /// <param name="primaryKey">Identifiant de la liste de référence.</param> /// <param name="defaultPropertyName">Nom de la propriété par défaut à utiliser. Null pour utiliser la valeur définie au niveau de l'objet.</param> /// <returns>Libellé de la liste de référence.</returns> public string GetReferenceValueByPrimaryKey(Type referenceType, object primaryKey, string defaultPropertyName = null) { object reference = GetReferenceObjectByPrimaryKey(referenceType, primaryKey); BeanDefinition definition = BeanDescriptor.GetDefinition(reference); BeanPropertyDescriptor property = string.IsNullOrEmpty(defaultPropertyName) ? definition.DefaultProperty : definition.Properties[defaultPropertyName]; return(property.ConvertToString(property.GetValue(reference))); }
public void ConvertToString() { BeanDefinition beanDefinition = BeanDescriptor.GetDefinition(new Bean()); BeanPropertyDescriptor primaryKey = beanDefinition.PrimaryKey; string text = primaryKey.ConvertToString(3); Assert.AreEqual("3", text); }
/// <summary> /// Appelle le PrimaryKeyAccessor pour le type donné avec la valeur de PK donnée. /// </summary> /// <param name="type">Type concerné.</param> /// <param name="primaryKey">Clef primaire.</param> /// <param name="propertyName">Propriété évaluée.</param> /// <returns>Libellé par défaut.</returns> private string InvokePrimaryKeyAccessor(Type type, object primaryKey, string propertyName = null) { object o = GetBeanByPrimaryKey(type, primaryKey); Accessor accessor = _primaryKeyAccessors[type.FullName]; BeanDefinition definition = BeanDescriptor.GetDefinition(accessor.ReturnType); BeanPropertyDescriptor property = string.IsNullOrEmpty(propertyName) ? definition.DefaultProperty : definition.Properties[propertyName]; return(property.ConvertToString(property.GetValue(o))); }
/// <summary> /// Ecrit la représentation Xml d'une propriété. /// </summary> /// <param name="xmlWriter">Writer XML.</param> /// <param name="property">Propriété.</param> /// <param name="propertyValue">Valeur de la propriété.</param> private static void WriteProperty(XmlTextWriter xmlWriter, BeanPropertyDescriptor property, object propertyValue) { ICollection collection = propertyValue as ICollection; if (property.PrimitiveType == typeof(byte[])) { xmlWriter.WriteStartElement(XmlNodeProperty); xmlWriter.WriteStartAttribute(XmlNameAttribute); xmlWriter.WriteName(property.PropertyName); xmlWriter.WriteEndAttribute(); if (propertyValue != null) { xmlWriter.WriteCData(Convert.ToBase64String((byte[])propertyValue)); } else { xmlWriter.WriteCData(string.Empty); } xmlWriter.WriteEndElement(); } else if (collection != null) { xmlWriter.WriteStartElement(XmlNodeCollection); xmlWriter.WriteStartAttribute(XmlNameAttribute); xmlWriter.WriteName(property.PropertyName); xmlWriter.WriteEndAttribute(); foreach (object data in collection) { WriteProperty(xmlWriter, property, data); } xmlWriter.WriteEndElement(); } else if (property.PrimitiveType == null) { xmlWriter.WriteStartElement(XmlNodeObject); xmlWriter.WriteStartAttribute(XmlNameAttribute); xmlWriter.WriteName(property.PropertyName); xmlWriter.WriteEndAttribute(); WriteProperties(xmlWriter, propertyValue); xmlWriter.WriteEndElement(); } else { xmlWriter.WriteStartElement(XmlNodeProperty); xmlWriter.WriteStartAttribute(XmlNameAttribute); xmlWriter.WriteName(property.PropertyName); xmlWriter.WriteEndAttribute(); if (propertyValue != null) { if (propertyValue.GetType() == typeof(ExtendedValue)) { xmlWriter.WriteCData(property.ConvertToString(propertyValue)); } else if (propertyValue.GetType() == typeof(DateTime)) { if (property.DomainName == "DO_JOUR_MOIS") { xmlWriter.WriteCData(((DateTime)propertyValue).ToString("dd/MM", DateTimeFormatInfo.CurrentInfo)); } else { xmlWriter.WriteCData(property.ConvertToString(propertyValue)); } } else if (propertyValue.GetType() == typeof(bool)) { xmlWriter.WriteCData(Convert.ToString(propertyValue, CultureInfo.CurrentCulture)); } else { xmlWriter.WriteCData(property.ConvertToString(propertyValue)); } } xmlWriter.WriteEndElement(); } }
/// <summary> /// Recursive get property value. /// </summary> /// <param name="dataSource">Source de données courante.</param> /// <param name="fieldName">Nom du Field.</param> /// <param name="isXmlData">Si la source en xml.</param> /// <returns>Value.</returns> protected object GetPropertyValue(object dataSource, string fieldName, bool isXmlData) { if (fieldName.Contains(".")) { string firstFieldName = fieldName.Substring(0, fieldName.IndexOf('.')); string lastFieldName = fieldName.Substring(fieldName.IndexOf('.') + 1); PropertyDescriptor property = TypeDescriptor.GetProperties(dataSource)[firstFieldName]; object newDataSource = property.GetValue(dataSource); return(GetPropertyValue(newDataSource, lastFieldName, isXmlData)); } else { if (isXmlData) { PropertyDescriptor property = TypeDescriptor.GetProperties(dataSource)[fieldName]; if (property != null) { return(property.GetValue(dataSource)); } else { throw new KeyNotFoundException("The tag " + this.TagName + " has no attribute named " + fieldName); } } else { BeanDefinition beanDefinition = BeanDescriptor.GetDefinition(dataSource); BeanPropertyDescriptor descriptor = beanDefinition.Properties[fieldName]; if (descriptor != null) { object propertyValue = descriptor.GetValue(dataSource); if (propertyValue == null) { return(null); } else { ICollection collection = propertyValue as ICollection; if (collection != null) { if (collection.Count == 0) { return(null); } else { return(propertyValue); } } else if (descriptor.PrimitiveType == typeof(byte[])) { return(propertyValue); } else if (propertyValue.GetType() == typeof(DateTime)) { if (descriptor.DomainName == "DO_JOUR_MOIS") { return(((DateTime)propertyValue).ToString("dd/MM", DateTimeFormatInfo.CurrentInfo)); } else { return(descriptor.ConvertToString(propertyValue)); } } else if (propertyValue.GetType() == typeof(bool)) { return(Convert.ToString(propertyValue, CultureInfo.InvariantCulture)); } else { return(descriptor.ConvertToString(propertyValue)); } } } else { throw new KeyNotFoundException("The tag " + this.TagName + " has no attribute named " + fieldName); } } } }