/// <summary> /// Reads the attributes of the current xml element and uses them to populute the target with /// </summary> /// <param name="target">The object which is populated with the xml attributes</param> /// <param name="reader">The xml stream</param> /// <param name="root">Top object of the xml stream</param> /// <exception cref="System.Exception">Cannot find class type</exception> /// <exception cref="System.Exception">Cannot instantiate object for known class type</exception> private static void ReadAttributes (IAggregate target, XmlReader reader, object root) { // Read all attributes string[] properties = target.Properties; for (int i = 0; i < properties.Length; i++) { string name = XmlFile.GetElementName (target.Source.GetType(), properties[i], false); if (name != null) { string attributeValue = reader.GetAttribute(name); if (attributeValue != null) { string classType = (string) MetaInfo.GetAttributeDefault (target.Source.GetType(), properties[i], "XmlType", target.GetType(properties[i]).FullName); Type type = ObjectSupport.GetType(classType); if (type == null) { throw new Exception("Could not find class type " + classType); } if (type.Equals(typeof(FileInfo))) { attributeValue = FileSupport.ExpandRelativePath (XmlFile.GetRegisteredFile(root).Directory, attributeValue).FullName; } if (type.Equals(typeof(DirectoryInfo))) { attributeValue = FileSupport.ExpandRelativeDirectory (XmlFile.GetRegisteredFile(root).Directory, attributeValue).FullName; } object targetValue = ObjectSupport.GetInstance (type, attributeValue, _culture); if (targetValue == null) { throw new Exception("Could not instantiate class type " + type.FullName); } if (target.CanWrite(properties[i])) { target.SetValue(properties[i], targetValue); } } } } }
/// <summary> /// Gets the key from an object. /// If the key isn't unique and the property allows for key generation, a unique key is generated. /// </summary> /// <param name="target"></param> /// <param name="root"></param> /// <returns></returns> private static string GetKeyFromObject(IAggregate target, object root) { string key = null; int lastkey = 0; string[] property = target.Properties; int generatedProperty = -1; bool generationPossible = true; while (generationPossible) { key = null; lastkey++; generationPossible = false; for (int i = 0; i < property.Length; i++) { if ((Boolean) MetaInfo.GetAttributeDefault (target.Source.GetType(), property[i], "XmlKey", false)) { object KeyObject = target.GetValue(property[i]); if ((KeyObject != null) && (!KeyObject.ToString().Trim().Equals(""))) { key += KeyObject.ToString() + ";"; } else if ((Boolean) MetaInfo.GetAttributeDefault (target.Source.GetType(), property[i], "XmlAllowGeneration", true)) { generationPossible = true; generatedProperty = i; key += lastkey + ";"; } } } if (key != null) { key += target.Source.GetType().FullName; object registeredObject = XmlFile.GetRegisteredTarget(root, key); if ((registeredObject == null) || (registeredObject == target)) { if (generatedProperty != -1) { Type PropertyType = target.GetType(property[generatedProperty]); if (PropertyType.Equals(typeof(string))) { target.SetValue(property[generatedProperty], Convert.ToString(lastkey)); } else if (PropertyType.Equals(typeof(Int16)) || PropertyType.Equals(typeof(Int32)) || PropertyType.Equals(typeof(Int64))) { target.SetValue(property[generatedProperty], lastkey); } } return key; } } } return key; }