/// <summary> /// Create an object of the specified Type using the values provided /// </summary> /// <param name="values">Values to assign to the object</param> /// <param name="type">The type of object to create</param> /// <returns>An Object of the type initialized with the specified Values</returns> public static object CreateObject(Dictionary <PropertyInfo, object> values, Type type) { // Check that an import template exists for this type if (!ImportTemplates.ContainsKey(type)) { throw new NotSupportedException($"Cannot create an import for type '{type}'"); } IImportTemplate importTemplate = ImportTemplates[type]; // Check that the values contain all required properties if (importTemplate.RequiredProperties != null && importTemplate.RequiredProperties.Length == 0) { foreach (var property in importTemplate.RequiredProperties) { if (!values.ContainsKey(property)) { throw new NullReferenceException($"You have not set all required values for an object of type '{type}'"); } } } object entity = importTemplate.CreateTemplateObject(); SetDatesOnTemplateObject(entity, type); // Set all of the values specified foreach (var valuePair in values) { var property = valuePair.Key; var value = valuePair.Value; // Check to make sure the ID of this field is not being set if (property.Name == $"{type.Name}ID") { throw new ArgumentException($"You cannot set the ID for a {type.Name}"); } // Make sure that they are not overriding an automatic value if (importTemplate.IgnoredProperties.Contains(property)) { continue; } property.SetValue(entity, value); } return(entity); }
/// <summary> /// Handles the RowCommand event of the grdTemplates control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewCommandEventArgs"/> instance containing the event data.</param> protected void grdTemplates_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName.Equals("Delete")) { { int rowIndex = Convert.ToInt32(e.CommandArgument); string id = grdTemplates.DataKeys[rowIndex].Value.ToString(); IImportTemplate importMap = EntityFactory.GetById <IImportTemplate>(id); if (importMap != null) { importMap.Delete(); LoadTemplates(); } } } }
/// <summary> /// Gets all the Import templates that have been written /// </summary> /// <returns></returns> private static Dictionary <Type, IImportTemplate> GetImportTemplates() { Dictionary <Type, IImportTemplate> importTemplates = new Dictionary <Type, IImportTemplate>(); // Check all types in the current assembly and return all any that implement ImportTemplate<> var importTemplateTypes = Assembly.GetExecutingAssembly().GetTypes().Where(type => IsImportTemplate(type)).ToArray(); foreach (var importTemplateType in importTemplateTypes) { // This is the type of object the import template creates (E.G Asset) var importType = importTemplateType.BaseType.GetGenericArguments()[0]; // The import template class itself IImportTemplate importTemplate = Activator.CreateInstance(importTemplateType) as IImportTemplate; importTemplates.Add(importType, importTemplate); // This converts the property names into property info's importTemplate.SetRequiredAndIgnoredProperties(); } return(importTemplates); }