Example #1
0
        public virtual void SetDefaultValue()
        {
            Type sectionType = this.GetType();

            IICConfigSectionAttribute sectionAttr = AttributeHelper.GetAttribute <IICConfigSectionAttribute>(sectionType);

            foreach (FieldInfo field in sectionType.GetFields())
            {
                IICConfigFieldAttribute fieldAttr = AttributeHelper.TryGetAttribute <IICConfigFieldAttribute>(field);
                if (fieldAttr != null && fieldAttr.DefaultValue != null)
                {
                    ObjectHelper.SetValue(field, this, fieldAttr.DefaultValue);
                    continue;
                }

                IICConfigItemAttribute itemAttr = AttributeHelper.TryGetAttribute <IICConfigItemAttribute>(field);
                if (itemAttr != null)
                {
                    IICConfigItem item = (IICConfigItem)Activator.CreateInstance(field.FieldType);
                    item.SetDefaultValue();
                    field.SetValue(this, item);
                    continue;
                }

                IICConfigItemCollectionAttribute colletionAttr = AttributeHelper.TryGetAttribute <IICConfigItemCollectionAttribute>(field);
                if (colletionAttr != null)
                {
                    object collection = Activator.CreateInstance(field.FieldType);
                    field.SetValue(this, collection);
                    continue;
                }
            }
        }
Example #2
0
        public static IICConfigItem CreateDefault(Type type)
        {
            IICConfigItemAttribute itemAttr = AttributeHelper.GetAttribute <IICConfigItemAttribute>(type);

            if (itemAttr.IsRequired)
            {
                throw new ConfigurationNotFoundException(IICConfigType.Item, type.Name);
            }
            else
            {
                IICConfigItem ret = (IICConfigItem)Activator.CreateInstance(type);
                ret.SetDefaultValue();
                return(ret);
            }
        }