internal protected virtual DataNode OnSerializeProperty (ItemProperty prop, SerializationContext serCtx, object instance, object value) { return prop.OnSerialize (serCtx, value); }
internal protected virtual object OnDeserializeProperty (ItemProperty prop, SerializationContext serCtx, object instance, DataNode data) { return prop.OnDeserialize (serCtx, data); }
public void ForceDefaultValueSerialization (ItemProperty prop) { if (forcedSerializationProps == null) forcedSerializationProps = new HashSet<ItemProperty> (); forcedSerializationProps.Add (prop); }
public bool IsDefaultValueSerializationForced (ItemProperty prop) { if (IncludeDefaultValues) return true; else if (forcedSerializationProps != null) return forcedSerializationProps.Contains (prop); else return false; }
public void RegisterProperty (Type targetType, string name, Type propertyType, bool isExternal, bool skipEmpty) { if (!typeof(IExtendedDataItem).IsAssignableFrom (targetType)) throw new InvalidOperationException ("The type '" + targetType + "' does not implement the IExtendedDataItem interface and cannot be extended with new properties"); ClassDataType ctype = (ClassDataType) GetConfigurationDataType (targetType); ItemProperty prop = new ItemProperty (name, propertyType); prop.Unsorted = true; prop.IsExternal = isExternal; prop.SkipEmpty = skipEmpty; ctype.AddProperty (prop); }
public void RegisterProperty (Type targetType, ItemProperty property) { if (!typeof(IExtendedDataItem).IsAssignableFrom (targetType)) throw new InvalidOperationException ("The type '" + targetType + "' does not implement the IExtendedDataItem interface and cannot be extended with new properties"); ClassDataType ctype = (ClassDataType) GetConfigurationDataType (targetType); ctype.AddProperty (property); }
void AddProperty (ItemProperty prop, string insertBefore) { if (!prop.IsNested) { foreach (ItemProperty p in sortedPoperties) { if (p.IsNested && p.NameList[0] == prop.Name) throw CreateNestedConflictException (prop, p); } } else { ItemProperty p = properties [prop.NameList[0]] as ItemProperty; if (p != null) throw CreateNestedConflictException (prop, p); } prop.SetContext (Context); if (properties.ContainsKey (prop.Name)) throw new InvalidOperationException ("Duplicate property '" + prop.Name + "' in class '" + ValueType); properties.Add (prop.Name, prop); if (insertBefore != null) { bool added = false; for (int n=0; n<sortedPoperties.Count; n++) { ItemProperty p = sortedPoperties [n]; if (p.MemberName == insertBefore) { sortedPoperties.Insert (n, prop); added = true; break; } } if (!added) sortedPoperties.Add (prop); } else if (prop.Unsorted) { int foundPos = sortedPoperties.Count; for (int n=0; n < sortedPoperties.Count; n++) { ItemProperty cp = (ItemProperty) sortedPoperties [n]; if (cp.Unsorted && prop.Name.CompareTo (cp.Name) < 0) { foundPos = n; break; } } sortedPoperties.Insert (foundPos, prop); } else sortedPoperties.Add (prop); if (subtypes != null && subtypes.Count > 0) { foreach (ClassDataType subtype in subtypes) subtype.AddProperty (prop); } }
public void AddProperty(ItemProperty prop) { AddProperty(prop, null); }
void AddProperty (object member, string name, Type memberType) { // Using inherit=false because if a base class already has an ItemProperty applied to the property // then that property will already have been added while copying props from the base class. object[] ats = Context.AttributeProvider.GetCustomAttributes (member, typeof(Attribute), false); ItemPropertyAttribute at = FindPropertyAttribute (ats, ""); if (at == null) return; ItemProperty prop = new ItemProperty (); prop.Name = (at.Name != null) ? at.Name : name; prop.ExpandedCollection = Context.AttributeProvider.IsDefined (member, typeof(ExpandedCollectionAttribute), true); prop.DefaultValue = at.DefaultValue; prop.IsExternal = at.IsExternal; prop.SkipEmpty = at.SkipEmpty; prop.ReadOnly = at.ReadOnly; prop.WriteOnly = at.WriteOnly; if (prop.ExpandedCollection) { ICollectionHandler handler = Context.GetCollectionHandler (memberType); if (handler == null) throw new InvalidOperationException ("ExpandedCollectionAttribute can't be applied to property '" + prop.Name + "' in type '" + ValueType + "' becuase it is not a valid collection."); memberType = handler.GetItemType (); prop.ExpandedCollectionHandler = handler; } if (at.ValueType != null) prop.PropertyType = at.ValueType; else prop.PropertyType = memberType; if (at.SerializationDataType != null) { try { prop.DataType = (DataType) Activator.CreateInstance (at.SerializationDataType, new object[] { prop.PropertyType } ); } catch (MissingMethodException ex) { throw new InvalidOperationException ("Constructor not found for custom data type: " + at.SerializationDataType.Name + " (Type propertyType);", ex); } } if (member is MemberInfo) { prop.Member = (MemberInfo) member; AddProperty (prop); } else { prop.InitValue = ((ItemMember)member).InitValue; AddProperty (prop, ((ItemMember)member).InsertBefore); } prop.Initialize (ats, ""); if (prop.ExpandedCollection && prop.DataType.IsSimpleType) throw new InvalidOperationException ("ExpandedCollectionAttribute is not allowed in collections of simple types"); }
public void AddProperty (ItemProperty prop) { AddProperty (prop, null); }
void Deserialize(SerializationContext serCtx, object obj, DataCollection itemData, DataItem ukwnDataRoot, string baseName) { Hashtable expandedCollections = null; foreach (DataNode value in itemData) { ItemProperty prop = (ItemProperty)properties [baseName + value.Name]; if (prop == null) { if (value is DataItem) { DataItem root = new DataItem(); root.Name = value.Name; root.UniqueNames = ((DataItem)value).UniqueNames; if (ukwnDataRoot != null) { ukwnDataRoot.ItemData.Add(root); } Deserialize(serCtx, obj, ((DataItem)value).ItemData, root, baseName + value.Name + "/"); // If no unknown data has been added, there is no need to keep this // in the unknown items list. if (ukwnDataRoot != null && !root.HasItemData) { ukwnDataRoot.ItemData.Remove(root); } } else if (obj is IExtendedDataItem && (value.Name != "ctype" || baseName.Length > 0)) { // store unreadable raw data to a special property so it can be // serialized back an the original format is kept // The ctype attribute don't need to be stored for the root object, since // it is generated by the serializer ukwnDataRoot.ItemData.Add(value); } continue; } if (prop.WriteOnly || !prop.CanDeserialize(serCtx, obj)) { continue; } try { if (prop.ExpandedCollection) { ICollectionHandler handler = prop.ExpandedCollectionHandler; if (expandedCollections == null) { expandedCollections = new Hashtable(); } object pos, col; if (!expandedCollections.ContainsKey(prop)) { col = handler.CreateCollection(out pos, -1); } else { pos = expandedCollections [prop]; col = prop.GetValue(obj); } handler.AddItem(ref col, ref pos, prop.Deserialize(serCtx, obj, value)); expandedCollections [prop] = pos; prop.SetValue(obj, col); } else { if (prop.HasSetter && prop.DataType.CanCreateInstance) { prop.SetValue(obj, prop.Deserialize(serCtx, obj, value)); } else if (prop.DataType.CanReuseInstance) { object pval = prop.GetValue(obj); if (pval == null) { if (prop.HasSetter) { throw new InvalidOperationException("The property '" + prop.Name + "' is null and a new instance of '" + prop.PropertyType + "' can't be created."); } else { throw new InvalidOperationException("The property '" + prop.Name + "' is null and it does not have a setter."); } } prop.Deserialize(serCtx, obj, value, pval); } else { throw new InvalidOperationException("The property does not have a setter."); } } } catch (Exception ex) { throw new InvalidOperationException("Could not set property '" + prop.Name + "' in type '" + Name + "'", ex); } } }
Exception CreateNestedConflictException(ItemProperty p1, ItemProperty p2) { return(new InvalidOperationException("There is a conflict between the properties '" + p1.Name + "' and '" + p2.Name + "'. Nested element properties can't be mixed with normal element properties.")); }
void AddProperty(ItemProperty prop, string insertBefore) { if (!prop.IsNested) { foreach (ItemProperty p in sortedPoperties) { if (p.IsNested && p.NameList[0] == prop.Name) { throw CreateNestedConflictException(prop, p); } } } else { ItemProperty p = properties [prop.NameList[0]] as ItemProperty; if (p != null) { throw CreateNestedConflictException(prop, p); } } prop.SetContext(Context); if (properties.ContainsKey(prop.Name)) { throw new InvalidOperationException("Duplicate property '" + prop.Name + "' in class '" + ValueType); } properties.Add(prop.Name, prop); if (insertBefore != null) { bool added = false; for (int n = 0; n < sortedPoperties.Count; n++) { ItemProperty p = sortedPoperties [n]; if (p.MemberName == insertBefore) { sortedPoperties.Insert(n, prop); added = true; break; } } if (!added) { sortedPoperties.Add(prop); } } else if (prop.Unsorted) { int foundPos = sortedPoperties.Count; for (int n = 0; n < sortedPoperties.Count; n++) { ItemProperty cp = (ItemProperty)sortedPoperties [n]; if (cp.Unsorted && prop.Name.CompareTo(cp.Name) < 0) { foundPos = n; break; } } sortedPoperties.Insert(foundPos, prop); } else { sortedPoperties.Add(prop); } if (subtypes != null && subtypes.Count > 0) { foreach (ClassDataType subtype in subtypes) { subtype.AddProperty(prop); } } }
internal protected virtual void OnDeserializeProperty (ItemProperty prop, SerializationContext serCtx, object instance, DataNode data, object valueInstance) { prop.OnDeserialize (serCtx, data, valueInstance); }
Exception CreateNestedConflictException (ItemProperty p1, ItemProperty p2) { return new InvalidOperationException ("There is a conflict between the properties '" + p1.Name + "' and '" + p2.Name + "'. Nested element properties can't be mixed with normal element properties."); }
internal protected virtual bool CanHandleProperty (ItemProperty property, SerializationContext serCtx, object instance) { return true; }
void AddProperty(object member, string name, Type memberType) { // Using inherit=false because if a base class already has an ItemProperty applied to the property // then that property will already have been added while copying props from the base class. object[] ats = Context.AttributeProvider.GetCustomAttributes(member, typeof(Attribute), false); ItemPropertyAttribute at = FindPropertyAttribute(ats, ""); if (at == null) { return; } ItemProperty prop = new ItemProperty(); prop.Name = (at.Name != null) ? at.Name : name; prop.ExpandedCollection = Context.AttributeProvider.IsDefined(member, typeof(ExpandedCollectionAttribute), true); prop.DefaultValue = at.DefaultValue; prop.IsExternal = at.IsExternal; prop.SkipEmpty = at.SkipEmpty; prop.ReadOnly = at.ReadOnly; prop.WriteOnly = at.WriteOnly; if (prop.ExpandedCollection) { ICollectionHandler handler = Context.GetCollectionHandler(memberType); if (handler == null) { throw new InvalidOperationException("ExpandedCollectionAttribute can't be applied to property '" + prop.Name + "' in type '" + ValueType + "' becuase it is not a valid collection."); } memberType = handler.GetItemType(); prop.ExpandedCollectionHandler = handler; } if (at.ValueType != null) { prop.PropertyType = at.ValueType; } else { prop.PropertyType = memberType; } if (at.SerializationDataType != null) { try { prop.DataType = (DataType)Activator.CreateInstance(at.SerializationDataType, new object[] { prop.PropertyType }); } catch (MissingMethodException ex) { throw new InvalidOperationException("Constructor not found for custom data type: " + at.SerializationDataType.Name + " (Type propertyType);", ex); } } if (member is MemberInfo) { prop.Member = (MemberInfo)member; AddProperty(prop); } else { prop.InitValue = ((ItemMember)member).InitValue; AddProperty(prop, ((ItemMember)member).InsertBefore); } prop.Initialize(ats, ""); if (prop.ExpandedCollection && prop.DataType.IsSimpleType) { throw new InvalidOperationException("ExpandedCollectionAttribute is not allowed in collections of simple types"); } }