Esempio n. 1
0
        private object LoadItem(string path, string key, Type itemType, IICIndex <IICConfigItemBuffer> index)
        {
            IICConfigItemAttribute itemAttr = AttributeHelper.GetAttribute <IICConfigItemAttribute>(itemType);
            object ret = Activator.CreateInstance(itemType);

            foreach (FieldInfo field in itemType.GetFields())
            {
                IICConfigFieldAttribute fieldAttr = AttributeHelper.TryGetAttribute <IICConfigFieldAttribute>(field);
                if (fieldAttr != null)
                {
                    IICConfigItemBuffer buffer = index.TryFindOne(path, key, fieldAttr.FieldName);
                    if (buffer != null)
                    {
                        ObjectHelper.SetValue(field, ret, buffer.Value, field.FieldType);
                    }
                    else if (fieldAttr.DefaultValue != null)
                    {
                        ObjectHelper.SetValue(field, ret, fieldAttr.DefaultValue);
                    }
                    else
                    {
                        throw new ConfigurationNotFoundException(path, key, field.Name);
                    }
                }
            }
            return(ret);
        }
Esempio n. 2
0
        private T LoadSection <T>(string sectionName, IICIndex <IICConfigItemBuffer> index) where T : IICConfigSection
        {
            IICConfigSectionAttribute sectionAttr = AttributeHelper.GetAttribute <IICConfigSectionAttribute>(typeof(T));
            T section = Activator.CreateInstance <T>();

            foreach (FieldInfo field in typeof(T).GetFields())
            {
                IICConfigFieldAttribute fieldAttr = AttributeHelper.TryGetAttribute <IICConfigFieldAttribute>(field);
                if (fieldAttr != null)
                {
                    IICConfigItemBuffer item = index.TryFindOne(sectionAttr.SectionName, string.Empty, fieldAttr.FieldName);
                    if (item != null)
                    {
                        ObjectHelper.SetValue(field, section, item.Value, field.FieldType);
                    }
                    else
                    {
                        ObjectHelper.SetValue(field, section, fieldAttr.DefaultValue);
                    }
                    continue;
                }

                IICConfigItemAttribute itemAttr = AttributeHelper.TryGetAttribute <IICConfigItemAttribute>(field);
                if (itemAttr != null)
                {
                    object item = LoadItem(sectionName + "." + itemAttr.ItemName, string.Empty, field.FieldType, index);
                    field.SetValue(section, item);
                    continue;
                }

                IICConfigItemCollectionAttribute colletionAttr = AttributeHelper.TryGetAttribute <IICConfigItemCollectionAttribute>(field);
                if (colletionAttr != null)
                {
                    Type[]            types      = field.FieldType.GetGenericArguments();
                    Type              keyType    = types[0];
                    Type              itemType   = types[1];
                    IConfigCollection collection = LoadCollection(sectionName + "." + colletionAttr.ItemName, keyType, itemType, field.FieldType, index);
                    field.SetValue(section, collection);
                    continue;
                }
            }
            return(section);
        }