示例#1
0
        ValueGenerator CreateValueGenerator(XElement settingElement, SettingLimits settingLimits = null)
        {
            ValueGenerator outValueGenerator        = null;
            bool           hasCreatedValueGenerator = false;

            //Iterate through all elements.
            IEnumerable <XElement> elements = settingElement.Elements();

            foreach (XElement element in elements)
            {
                //Warn about more than one value element.
                if (hasCreatedValueGenerator)
                {
                    _errorCollection.AddInvalidElement(element);
                    continue;
                }

                //Handle value element.
                ValueGeneratorTypes valueGeneratorType;
                if (Enum.TryParse <ValueGeneratorTypes>(element.Name.LocalName, out valueGeneratorType))
                {
                    switch (valueGeneratorType)
                    {
                    case ValueGeneratorTypes.Null:
                        outValueGenerator = null;
                        break;

                    case ValueGeneratorTypes.Constant:
                        outValueGenerator = CreateConstantValueGenerator(element);
                        break;

                    case ValueGeneratorTypes.Range:
                        outValueGenerator = CreateRangeValueGenerator(element);
                        break;

                    case ValueGeneratorTypes.Boolean:
                        outValueGenerator = CreateBooleanValueGenerator(element);
                        break;

                    case ValueGeneratorTypes.CoinFlip:
                        outValueGenerator = CreateCoinFlipValueGenerator(element);
                        break;

                    case ValueGeneratorTypes.WeightedSelector:
                        outValueGenerator = CreateWeightedSelectorValueGenerator(element, settingLimits);
                        break;

                    default:
                        _errorCollection.AddInvalidElement(element);
                        break;
                    }

                    hasCreatedValueGenerator = outValueGenerator != null || valueGeneratorType == ValueGeneratorTypes.Null;

                    if (outValueGenerator != null && settingLimits != null && !outValueGenerator.IsValueRangeWithinLimits(settingLimits))
                    {
                        _errorCollection.AddSettingOutsideLimits(element, settingLimits);
                    }
                }
                //Invalid element.
                else
                {
                    _errorCollection.AddInvalidElement(element);
                }
            }

            return(outValueGenerator);
        }