/// <summary> /// Saves the ConfigObject to a ConfigNode, then returns it /// The name of the created ConfigNode is taken from the NodeName property /// </summary> /// <returns>The created ConfigNode containing the saved data</returns> public ConfigNode Save() { //Create the ConfigNode ConfigNode node = new ConfigNode(this.NodeName); //Treat each member individually foreach (MemberInfo mi in this.members) { //Get ConfigFieldAttribute ConfigFieldAttribute attribute = (ConfigFieldAttribute)mi.GetCustomAttributes(ATTRIBUTE_TYPE, false)[0]; //Get custom name if any, or default name string valueName = string.IsNullOrEmpty(attribute.CustomName) ? mi.Name : attribute.CustomName; //Handle fields and properties switch (mi) { case FieldInfo fi: AddValue(node, fi.GetValue(this), valueName); break; case PropertyInfo pi: AddValue(node, pi.GetValue(this, null), valueName); break; } } //Return final node return(node); }
/// <summary> /// Loads data from the given ConfigNode to the object /// </summary> /// <param name="node">ConfigNode to load data from</param> public void Load(ConfigNode node) { //Make sure the passed node isn't null if (node == null) { throw new ArgumentNullException(nameof(node), "Provided ConfigNode is null"); } //Treat each member individually foreach (MemberInfo mi in this.members) { //Get ConfigFieldAttribute ConfigFieldAttribute attribute = (ConfigFieldAttribute)mi.GetCustomAttributes(ATTRIBUTE_TYPE, false)[0]; //Get custom name if any, or default name string valueName = string.IsNullOrEmpty(attribute.CustomName) ? mi.Name : attribute.CustomName; //Handle fields and properties switch (mi) { case FieldInfo fi: fi.SetValue(this, GetValue(node, valueName, fi.FieldType, attribute.Mandatory)); break; case PropertyInfo pi: pi.SetValue(this, GetValue(node, valueName, pi.PropertyType, attribute.Mandatory), null); break; } } }