/// <summary> /// Gets an instance of <see cref="GspCoreEntity"/> that contains data from the <core .../> section of /// galleryserverpro.config. The entity can be updated with new values and then passed to the <see cref="SaveCore"/> /// method for persisting back to the file system. /// </summary> /// <param name="core">An instance of <see cref="GalleryServerPro.Configuration.Core"/> that contains configuration /// data stored in the <core .../> section of galleryserverpro.config.</param> /// <returns>Returns an instance of <see cref="GspCoreEntity"/> that is contains data from the <core .../> section of /// galleryserverpro.config.</returns> public static GspCoreEntity GetGspCoreEntity(Core core) { GspCoreEntity gce = new GspCoreEntity(); Type coreType = typeof(Core); Type gceType = typeof(GspCoreEntity); // Iterate through each property of the core parameter and copy the value to the entity. Each property of the entity // has been configurated so that its data type matches that in the parameter. foreach (PropertyInfo coreProperty in coreType.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)) { System.Diagnostics.Debug.WriteLine(coreProperty.Name); FieldInfo aseProperty = gceType.GetField(ToFirstCharLower(coreProperty.Name)); if (aseProperty.FieldType == typeof(bool)) { aseProperty.SetValue(gce, Convert.ToBoolean(coreProperty.GetValue(core, null), System.Globalization.CultureInfo.InvariantCulture)); } else if (aseProperty.FieldType == typeof(string)) { aseProperty.SetValue(gce, Convert.ToString(coreProperty.GetValue(core, null), System.Globalization.CultureInfo.InvariantCulture)); } else if (aseProperty.FieldType == typeof(int)) { aseProperty.SetValue(gce, Convert.ToInt32(coreProperty.GetValue(core, null), System.Globalization.CultureInfo.InvariantCulture)); } else if (aseProperty.FieldType == typeof(Single)) { aseProperty.SetValue(gce, Convert.ToSingle(coreProperty.GetValue(core, null), System.Globalization.CultureInfo.InvariantCulture)); } else { throw new ArgumentOutOfRangeException(string.Format("GspConfigController.GetGspCoreEntity is not designed to process a property of type {0} (encountered in GspCoreEntity.{1})", aseProperty.FieldType, aseProperty.Name)); } } return gce; }
/// <summary> /// Persist the configuration data to the <core .../> section of galleryserverpro.config. /// </summary> /// <param name="core">An instance of <see cref="GspCoreEntity"/> that contains data to save to the /// <core .../> section of galleryserverpro.config.</param> /// <exception cref="UnauthorizedAccessException">Thrown when the IIS application pool identity does not have /// write access to galleryserverpro.config.</exception> public static void SaveCore(GspCoreEntity core) { XmlDocument xmlDoc = LoadGalleryServerProConfigFromDisk(); // Update the attributes of the <core ...> element with the values from the source config file. XmlElement coreElement = (XmlElement)xmlDoc.SelectSingleNode(@"galleryServerPro/core"); // Loop through each entity property and assign the value to the matching element in the <core> section of // galleryserverpro.config. Type type = typeof(GspCoreEntity); foreach (FieldInfo field in type.GetFields()) { string attValue = Convert.ToString(field.GetValue(core), System.Globalization.CultureInfo.InvariantCulture); if (field.FieldType == typeof(bool)) { // Bool fields should be stored in lower case; all others can be stored without modification. attValue = attValue.ToLowerInvariant(); } coreElement.SetAttribute(field.Name, attValue); } SaveGalleryServerProConfigToDisk(xmlDoc); }