Exemple #1
0
        private static void Load()
        {
            var assembly    = AppDomain.CurrentDomain.GetAssemblies().First(a => a.FullName.Contains("Reflow.Models"));
            var types       = assembly.GetTypes();
            var optionTypes = types.Where(t => typeof(ITagOption).IsAssignableFrom(t) && !t.IsInterface && !t.IsAbstract);
            var tags        = types.Where(t => typeof(ITag).IsAssignableFrom(t) && !t.IsInterface && !t.IsAbstract);

            var tagId = TagKey;

            foreach (var tag in tags)
            {
                var props         = tag.GetProperties();
                var reflowOptions = props.Where(p => p.IsDefined(typeof(ReflowOptionAttribute), false));

                var optionId = OptionKey;
                foreach (var option in reflowOptions)
                {
                    ReflowOptionAttribute attributeValue = TryGetAttributeValue(option);

                    var optionType    = TryGetType(option, attributeValue);
                    var optionDefault = TryGetDefault(option, attributeValue);
                    var optionName    = attributeValue.Name ?? option.Name;

                    var key = tagId + optionId;
                    optionId += OptionKey;
                    if (attributeValue is ReflowCollectionOptionAttribute)
                    {
                        var enabledOptions  = (attributeValue as ReflowCollectionOptionAttribute).EnabledValues.Select((o, idx) => (CollectionItem)Activator.CreateInstance(typeof(CollectionItem), idx, o, true));
                        var disabledOptions = (attributeValue as ReflowCollectionOptionAttribute).DisabledValues.Select((o, idx) => (CollectionItem)Activator.CreateInstance(typeof(CollectionItem), idx, o, false));
                        IEnumerable <CollectionItem> enumGeneratedOptions = TryGetEnumOptions(option);
                        _options.Add(key, (ITagOption)Activator.CreateInstance(optionType, key, optionName, optionDefault, enabledOptions.Union(disabledOptions).Union(enumGeneratedOptions).ToList()));
                    }
                    else
                    {
                        _options.Add(key, (ITagOption)Activator.CreateInstance(optionType, key, optionName, optionDefault));
                    }
                }
                var tagName       = (ReflowTagAttribute)tag.GetCustomAttribute(typeof(ReflowTagAttribute));
                var attributeName = tagName?.Name ?? tag.Name;

                _tags.Add(tagId, tag);

                tagId += TagKey;
            }
        }
Exemple #2
0
        private static Type TryGetType(PropertyInfo option, ReflowOptionAttribute attributeValue)
        {
            try
            {
                if (attributeValue.OptionType == null)
                {
                    if (_typeMappings.ContainsKey(option.PropertyType))
                    {
                        return(_typeMappings[option.PropertyType].DefaultType);
                    }

                    return(_typeMappings[option.PropertyType.BaseType].DefaultType);
                }
                return(attributeValue.OptionType);
            }
            catch (Exception)
            {
                return(null);
            }
        }