示例#1
0
        protected override TCollection ParseValue(object value)
        {
            if (value is TCollection asT)
            {
                if (IsValueAllowed(asT))
                {
                    return(asT);
                }
                else if (Value is TCollection currentValue && IsValueAllowed(currentValue))
                {
                    return(currentValue);
                }
            }

            // if null is allowed, return null
            if (PredefinedValues.Any(x => x == null))
            {
                return(null);
            }

            // otherwise create a new instance
            var newInstance = InstantiateObject <TCollection>();

            return(newInstance);
        }
        protected override T ParseValue(object value)
        {
            if (value is T asT)
            {
                if (IsValueAllowed(asT))
                {
                    if (!ReferenceEquals(Value, asT) || !SubEditables.Any())
                    {
                        CreateSubEditables(asT);
                    }
                    return(asT);
                }
                else if (Value is T currentValue && IsValueAllowed(currentValue))
                {
                    return(currentValue);
                }
            }

            // if null is allowed, return null
            var isNullAllowed = PredefinedValues.Any(x => x == null);

            if (isNullAllowed)
            {
                return(null);
            }

            var predefinedValuesOfT = PredefinedValues.OfType <T>().ToList();

            if (predefinedValuesOfT.Any())
            {
                return(predefinedValuesOfT.First());
            }

            // otherwise create a new instance
            var newInstance = (T)InstantiateObjectOfSpecificType(GetPredefinedType());

            CreateSubEditables(newInstance);
            return(newInstance);
        }