Beispiel #1
0
        /// <param name="type">Type to create.</param>
        /// <param name="chainer">Handles callback behavior for child values.</param>
        /// <inheritdoc cref="Create(Type,Func{object,bool})"/>
        private object Create(Type type, RandomizerChainer chainer)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            (bool, object)result = _hints
                                   .Select(h => h.TryCreate(type, chainer))
                                   .FirstOrDefault(r => r.Item1);

            if (!result.Equals(default))
        /// <summary>Initializes a new instance of the <see cref="RandomizerChainer"/> class.</summary>
        /// <param name="prevChainer">Previous chainer to build upon.</param>
        /// <param name="parent">Container of the instance to create.</param>
        private RandomizerChainer(RandomizerChainer prevChainer, object parent)
        {
            Parent      = parent;
            Gen         = prevChainer.Gen;
            _faker      = prevChainer._faker;
            _randomizer = prevChainer._randomizer;

            if (parent != null)
            {
                _history = prevChainer._history
                           .Append(new KeyValuePair <Type, object>(parent.GetType(), parent))
                           .ToDictionary(p => p.Key, p => p.Value);
            }
            else
            {
                _history = prevChainer._history;
            }
        }
Beispiel #3
0
        /// <inheritdoc/>
        protected internal sealed override (bool, object) TryCreate(Type type, RandomizerChainer randomizer)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }
            if (randomizer == null)
            {
                throw new ArgumentNullException(nameof(randomizer));
            }

            if (type.IsInheritedBy <T>() &&
                (type != typeof(object) || typeof(T) == typeof(object)) &&
                !randomizer.AlreadyCreated <T>())
            {
                return(true, Create(randomizer));
            }
            else
            {
                return(false, null);
            }
        }
Beispiel #4
0
 /// <summary>Tries to create a random instance of the given type.</summary>
 /// <param name="type">Type to generate.</param>
 /// <param name="randomizer">Handles callback behavior for child values.</param>
 /// <returns>
 ///     (<c>true</c>, created instance) if successful;
 ///     (<c>false</c>, <c>null</c>) otherwise.
 /// </returns>
 protected internal abstract (bool, object) TryCreate(Type type, RandomizerChainer randomizer);
Beispiel #5
0
 /// <summary>Creates a random <typeparamref name="T"/> instance.</summary>
 /// <param name="randomizer">Handles callback behavior for child values.</param>
 /// <returns>The created <typeparamref name="T"/> instance.</returns>
 protected abstract T Create(RandomizerChainer randomizer);