private static void Set(this XDocument document, IXmlEntity entity, PropertyInfo propertyInfo) { var propertyName = propertyInfo.Name; Type type = propertyInfo.PropertyType; try { if (type == typeof(string)) { document.SetString((string)propertyInfo.GetValue(entity), propertyName); } if (type == typeof(int) || type == typeof(int?)) { document.SetInt((int?)propertyInfo.GetValue(entity), propertyName); } if (type == typeof(double) || type == typeof(double?)) { document.SetDouble((double?)propertyInfo.GetValue(entity), propertyName); } if (type == typeof(bool) || type == typeof(bool?)) { document.SetBool((bool?)propertyInfo.GetValue(entity), propertyName); } if (type == typeof(DateTime) || type == typeof(DateTime?)) { document.SetDateTime((DateTime?)propertyInfo.GetValue(entity), propertyName); } if (type == typeof(DateTimeOffset) || type == typeof(DateTimeOffset?)) { document.SetDateTimeOffset((DateTimeOffset?)propertyInfo.GetValue(entity), propertyName); } if (type == typeof(IEnumerable <string>) || type == typeof(IList <string>)) { document.SetStrings((IEnumerable <string>)propertyInfo.GetValue(entity), propertyName); } if (type == typeof(IEnumerable <int>) || type == typeof(IList <int>)) { document.SetInts((IEnumerable <int>)propertyInfo.GetValue(entity), propertyName); } if (type == typeof(IEnumerable <double>) || type == typeof(IList <double>)) { document.SetDoubles((IEnumerable <double>)propertyInfo.GetValue(entity), propertyName); } if (type == typeof(IDictionary <string, double>)) { document.SetDictionaryStringDouble((IDictionary <string, double>)propertyInfo.GetValue(entity), propertyName); } if (type == typeof(IDictionary <string, string>)) { document.SetDictionaryStringString((IDictionary <string, string>)propertyInfo.GetValue(entity), propertyName); } return; } catch (InvalidCastException) { //TODO } throw new NotSupportedException($"Type {type.FullName} is not supported"); }
public static void UpdatePreCommitValuesXml(this IXmlEntity entity) { if (entity == null) { throw new ArgumentNullException(nameof(entity)); } entity.XmlValues = entity.GetXmlValuesFromProperties(typeof(XmlMappedAttribute)); }
public static void FillPropertiesFromXmlValues(this IXmlEntity entity, Type xmlMappedAttributeType, string xmlValues) { var document = xmlValues == null ? new XDocument(new XElement(RootNodename)) : XDocument.Parse(xmlValues); var properties = entity.GetXmlMappedProperties(xmlMappedAttributeType); foreach (var propertyInfo in properties) { propertyInfo.SetValue(entity, document.Get(propertyInfo)); } }
public static string GetXmlValuesFromProperties(this IXmlEntity entity, Type xmlMappedAttributeType) { var properties = entity.GetXmlMappedProperties(xmlMappedAttributeType); XDocument document = new XDocument(new XElement(RootNodename)); foreach (var propertyInfo in properties) { document.Set(entity, propertyInfo); } var rootNode = document.Root; if (rootNode == null || !rootNode.Elements().Any()) { return(null); } return(document.ToString(SaveOptions.DisableFormatting)); }
private static IEnumerable <PropertyInfo> GetXmlMappedProperties(this IXmlEntity entity, Type xmlMappedAttributeType) { return(entity.GetType().GetRuntimeProperties() .Where(prop => prop.IsDefined(xmlMappedAttributeType, false))); }
private void WriteToFile(string path, IXmlEntity entity) { var fi = new FileInfo(path); if (!fi.Directory.Exists) fi.Directory.Create(); using (var file = new StreamWriter(path)) { file.Write(entity.Xml); } }