/** Reads a list of dataobjects from this nodes child of given name, returns null if not found. Can be referenced, or full. */ protected List <T> ReadNamedDataObjectList <T>(XElement node, string name, FieldReferenceType referenceType, DataLibrary <T> library = null) where T : NamedDataObject { node = node.Element(name); if (node == null) { return(null); } var list = new List <T>(); string subNodeName = GetClassAttributesForType(typeof(T)).NodeName; library = library ?? NamedDataObject.GetLibraryForType <T>(); if (library == null && referenceType != FieldReferenceType.Full) { throw new Exception("Can not load type " + typeof(T) + " as no default library is defined, and it is stored referenced."); } foreach (var subNode in node.Elements(subNodeName)) { T item = null; switch (referenceType) { case FieldReferenceType.Full: item = (T)Activator.CreateInstance(typeof(T)); item.ReadNode(subNode); break; case FieldReferenceType.ID: var fieldId = int.Parse(subNode.Value); item = (T)library._byID(fieldId); break; case FieldReferenceType.Name: var fieldName = subNode.Value; item = (T)library._byName(fieldName); break; } list.Add(item); } return(list); }
/** * Adds a reference linking given library to given NamedDataObject type. This use used to link up objects * during deserialization. */ public static void AddGlobalLibrary <T>(DataLibrary <T> library) where T : NamedDataObject { GlobalLibrary[typeof(T)] = library; }