Exemplo n.º 1
0
        /// <summary>
        /// Gets all the properties of a type that can be edited
        /// </summary>
        /// <param name="type">Type to get the editable properties from</param>
        public static PropertyInfo[] GetEditableProperties(Type type)
        {
            if (!ImportTemplates.ContainsKey(type))
            {
                throw new NotSupportedException($"'{type.Name}' is not a supported Import Type");
            }

            if (!EditableProperties.ContainsKey(type))
            {
                PropertyInfo[]      allProps      = type.GetProperties();
                List <PropertyInfo> editableProps = new List <PropertyInfo>();

                foreach (var prop in allProps)
                {
                    if (IsEditableProperty(prop))
                    {
                        editableProps.Add(prop);
                    }
                }

                EditableProperties.Add(type, editableProps.ToArray());
            }

            return(EditableProperties[type]);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets all the properties required for a type
        /// </summary>
        /// <param name="type">The type to Test</param>
        public static PropertyInfo[] GetRequiredProperties(Type type)
        {
            if (!ImportTemplates.ContainsKey(type))
            {
                throw new NotSupportedException($"'{type.Name}' is not a supported Import Type");
            }

            return(ImportTemplates[type].RequiredProperties);
        }
Exemplo n.º 3
0
        /// <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);
        }