コード例 #1
0
ファイル: OptionsEntry.cs プロジェクト: pether-pg/ONIMods
        /// <summary>
        /// Builds the options entries from the type.
        /// </summary>
        /// <param name="forType">The type of the options class.</param>
        /// <returns>A list of all public properties annotated for options dialogs.</returns>
        internal static IDictionary <string, OptionsList> BuildOptions(Type forType)
        {
            var                    entries = new SortedList <string, OptionsList>(8);
            OptionAttribute        oa;
            DynamicOptionAttribute doa;

            foreach (var prop in forType.GetProperties())
            {
                // Must have the annotation
                foreach (var attr in prop.GetCustomAttributes(false))
                {
                    if ((oa = OptionAttribute.CreateFrom(attr)) != null)
                    {
                        // Attempt to find a class that will represent it
                        var entry = CreateOptions(prop, oa);
                        if (entry != null)
                        {
                            AddToCategory(entries, entry);
                        }
                        break;
                    }
                    else if ((doa = DynamicOptionAttribute.CreateFrom(attr)) != null)
                    {
                        AddToCategory(entries, DynamicOptionsEntry.Create(prop.Name, doa));
                        break;
                    }
                }
            }
            return(entries);
        }
コード例 #2
0
        /// <summary>
        /// Creates a new dynamic options entry from a [DynamicOption] attribute.
        /// </summary>
        /// <param name="name">The property name.</param>
        /// <param name="attr">The attribute giving the type of the handler.</param>
        /// <returns>A dynamic options entry for that type.</returns>
        internal static DynamicOptionsEntry Create(string name, DynamicOptionAttribute attr)
        {
            var    targetType = attr.Handler;
            object handler;

            if (targetType == null)
            {
                throw new ArgumentNullException("targetType");
            }
            try {
                handler = Activator.CreateInstance(targetType);
            } catch (Exception e) {
                // Log error
                PUtil.LogError("Cannot create options handler for " + name + ":");
                PUtil.LogException(e.GetBaseException() ?? e);
                handler = null;
            }
            return(new DynamicOptionsEntry(name, attr.Category, handler));
        }