コード例 #1
0
        public static IPropertyState <T> RegisterOption <T>(OptionCategory category, string name, T defaultValue)
        {
            if (!Enum.IsDefined(typeof(OptionCategory), category))
            {
                throw new ArgumentException("Invalid category.");
            }

            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentException("Option registration is empty.", "name");
            }

            if (name.IndexOfAny(new[] { '.', '\\', ' ', ':' }) > 0)
            {
                throw new ArgumentException("Option registration contains invalid characters.", "name");
            }

            string categoryName = Enum.GetName(typeof(OptionCategory), category);
            string optionName   = String.Format("{0}.{1}", categoryName, name);

            IPropertyState <T> state = null;
            var propertyName         = PropertyManager.CreatePropertyName(optionName);

            if (c_registeredProperties.ContainsKey(propertyName))
            {
                state = c_registeredProperties[propertyName] as IPropertyState <T>;
                if (state == null)
                {
                    throw new Exception("Duplicate option registration with a different type for {0}.");
                }

                WriteLine("Duplicate option registration: ", propertyName);
            }
            else
            {
                state = PropertyManager.Create(propertyName, defaultValue);
                c_registeredProperties.Add(propertyName, state);
                c_registeredPropertiesList.Add(state);
            }

            return(state);
        }