/// <summary> /// Creates a DbCollection that contains the XML elements /// </summary> /// <param name="xml">The XML.</param> /// <returns></returns> public static NonObservableDbCollection <T> FromXml(string xml) { return(new NonObservableDbCollection <T>( XmlDataRecord.TryParse(xml, typeof(T), false) .CreateListOfItems() .Select(item => typeof(T) .GetClassInfo() .SetPropertysViaReflection(item)))); }
public void GetName() { var xmlSerilizer = new XmlSerializer(typeof(InstanceMock)); var content = ""; using (var ms = new MemoryStream()) { xmlSerilizer.Serialize(ms, new InstanceMock()); content = Encoding.Default.GetString(ms.ToArray()); } var xmlRecord = new XmlDataRecord(content, typeof(InstanceMock).GetClassInfo()); Assert.AreEqual(xmlRecord.GetName(0), "MockPropA"); Assert.AreEqual(xmlRecord.GetName(1), "MockPropB"); }
public void GetValue() { var xmlSerilizer = new XmlSerializer(typeof(InstanceMock)); var content = ""; using (var ms = new MemoryStream()) { xmlSerilizer.Serialize(ms, new InstanceMock()); content = Encoding.Default.GetString(ms.ToArray()); } var dbConfig = new DbConfig(true); var xmlRecord = new XmlDataRecord(content, dbConfig.GetOrCreateClassInfoCache(typeof(InstanceMock))); Assert.AreEqual(xmlRecord.GetValue(0), "NAN"); Assert.AreEqual(xmlRecord.GetValue(1), 0); }
/// <summary> /// Creates a new Instance based on possible Ctor's and the given /// <paramref name="reader" /> /// </summary> /// <returns></returns> public static object SetPropertiesViaReflection( this DbClassInfoCache type, XmlDataRecord reader, DbAccessType?accessType = null, DbConfig config = null) { if (reader == null) { return(null); } var eagerReader = new EagarDataRecord(); for (int i = 0; i < reader.FieldCount; i++) { eagerReader.Add(eagerReader.GetName(i), eagerReader[i]); } bool created; var source = DbAccessLayer.CreateInstance(type, eagerReader, out created); if (created) { return(source); } if (config == null) { config = new DbConfig(true); } #pragma warning disable 618 return(DbAccessLayer.ReflectionPropertySet(config, source, type, eagerReader, new DbAccessLayer.ReflectionSetCacheModel(), accessType)); #pragma warning restore 618 }
public static object ReflectionPropertySet( DbConfig config, object instance, DbClassInfoCache info, IDataRecord reader, Dictionary <int, DbPropertyInfoCache> cache, DbAccessType?dbAccessType) { if (instance == null) { throw new ArgumentNullException("instance"); } if (info == null) { throw new ArgumentNullException("info"); } if (reader == null) { return(instance); } //Left c# property name and right the object to read from the reader //var listofpropertys = new Dictionary<string, object>(); var propertys = info.Propertys.ToArray(); var instanceOfFallbackList = new Dictionary <string, object>(); if (cache == null) { cache = new Dictionary <int, DbPropertyInfoCache>(); for (var i = 0; i < reader.FieldCount; i++) { DbPropertyInfoCache val = null; info.Propertys.TryGetValue(info.SchemaMappingDatabaseToLocal(reader.GetName(i)), out val); cache.Add(i, val); } } for (var i = 0; i < reader.FieldCount; i++) { var property = cache[i]; var value = reader.GetValue(i); if (property != null) { var attributes = property.Attributes; var valueConverterAttributeModel = attributes.FirstOrDefault(s => s.Attribute is ValueConverterAttribute); //Should the SQL value be converted if (valueConverterAttributeModel != null) { var converter = valueConverterAttributeModel.Attribute as ValueConverterAttribute; //Create the converter and then convert the value before everything else var valueConverter = converter.CreateConverter(); value = valueConverter.Convert(value, property.PropertyInfo.PropertyType, converter.Parameter, CultureInfo.CurrentCulture); } var xmlAttributeModel = attributes.FirstOrDefault(s => s.Attribute is FromXmlAttribute); //should the Content be considerd as XML text? if (xmlAttributeModel != null) { //Get the XML text and check if its null or empty var xmlStream = value.ToString(); if (string.IsNullOrEmpty(xmlStream)) { continue; } //Check for List //if this is a list we are expecting other entrys inside if (property.CheckForListInterface()) { //target Property is of type list //so expect a xml valid list Take the first element and expect the propertys inside this first element var record = XmlDataRecord.TryParse(xmlStream, property.PropertyInfo.PropertyType.GetGenericArguments().FirstOrDefault(), false, config); var xmlDataRecords = record.CreateListOfItems(); var genericArguments = config.GetOrCreateClassInfoCache( property.PropertyInfo.PropertyType.GetGenericArguments().FirstOrDefault()); var enumerableOfItems = xmlDataRecords.Select( s => genericArguments.SetPropertysViaReflection(s, dbAccessType, config)).ToList(); object castedList; if (genericArguments.Type.IsClass && genericArguments.Type.GetInterface("INotifyPropertyChanged") != null) { var caster = typeof(DbCollection <>).MakeGenericType(genericArguments.Type) .GetConstructor(new[] { typeof(IEnumerable) }); castedList = caster.Invoke(new object[] { enumerableOfItems }); } else { var caster = typeof(NonObservableDbCollection <>).MakeGenericType(genericArguments.Type) .GetConstructor(new[] { typeof(IEnumerable) }); castedList = caster.Invoke(new object[] { enumerableOfItems }); } property.Setter.Invoke(instance, castedList); } else { var classInfo = config.GetOrCreateClassInfoCache(property .PropertyInfo .PropertyType); var xmlDataRecord = XmlDataRecord.TryParse(xmlStream, property.PropertyInfo.PropertyType, true, config); //the t var xmlSerilizedProperty = classInfo.SetPropertysViaReflection(xmlDataRecord, dbAccessType, config); property.Setter.Invoke(instance, xmlSerilizedProperty); } } else if (value is DBNull || value == null) { property.Setter.Invoke(instance, new object[] { null }); } else { object changedType; if (value.GetType() != property.PropertyInfo.PropertyType) { changedType = DataConverterExtensions.ChangeType(value, property.PropertyInfo.PropertyType); } else { changedType = value; } property.Setter.Invoke(instance, changedType); } } //This variable is null if we tried to find a property with the LoadNotImplimentedDynamicAttribute but did not found it else if (instanceOfFallbackList != null) { //no property found Look for LoadNotImplimentedDynamicAttribute property to include it if (instanceOfFallbackList.Any()) { instanceOfFallbackList.Add(reader.GetName(i), value); } else { var maybeFallbackProperty = propertys.FirstOrDefault( s => s.Value.Attributes.Any(e => e.Attribute is LoadNotImplimentedDynamicAttribute)); if (maybeFallbackProperty.Value != null) { instanceOfFallbackList = (Dictionary <string, object>)maybeFallbackProperty.Value.Getter.Invoke(instance); if (instanceOfFallbackList == null) { instanceOfFallbackList = new Dictionary <string, object>(); maybeFallbackProperty.Value.Setter.Invoke(instance, instanceOfFallbackList); } instanceOfFallbackList.Add(reader.GetName(i), value); } else { instanceOfFallbackList = null; } } } } //foreach (var item in listofpropertys) //{ // var property = propertys.FirstOrDefault(s => s.PropertyName == item.Key); //} return(instance); }