Пример #1
0
        private void InitializeGenerators()
        {
            _randomLengthGenerator = _generatorFactory.CreateCollectionSizeGenerator(_seed);

            if (TypeCheckExtensions.IsTypeNumericOrChar(_typeParameter))
            {
                _randomValueGenerator = _generatorFactory.CreateNumericGenerator(_seed);
            }
            else if (_typeParameter == typeof(string))
            {
                _randomValueGenerator = _generatorFactory.CreateStringGenerator(_seed);
            }
            else if (_typeParameter == typeof(bool))
            {
                _randomValueGenerator = _generatorFactory.CreateBooleanGenerator(_seed);
            }
            else if (_typeParameter == typeof(DateTime))
            {
                _randomValueGenerator = _generatorFactory.CreateDateTimeGenerator(_seed);
            }

            // Just for sake of not making this too complicated let's assume that if a type
            // implements IEnumerable then it's a collection
            // We've also already checked if the type is string, so it's safe to just check IEnumerable
            else if (_depthLimit >= 0 && _typeParameter.GetInterface(nameof(IEnumerable)) != null)
            {
                _randomValueGenerator = _generatorFactory.CreateCollectionGenerator(_typeParameter, (int)(_seed + _depthLimit), _depthLimit);
            }

            else if (_depthLimit > 0 && _typeParameter.IsClass && !(typeof(Delegate).IsAssignableFrom(_typeParameter)) && _typeParameter.GetConstructor(Type.EmptyTypes) != null)
            {
                _randomValueGenerator = _generatorFactory.CreateObjectGenerator(_typeParameter, _seed, _depthLimit - 1);
            }

            // We can't assign value types other than we know about or types without a parameterless constructor
            else
            {
                _randomValueGenerator = null;
            }
        }
Пример #2
0
        public IEnumerable <object> GetMockObjects(string typeName, int skip, int take)
        {
            var masterSeed = _options.Value.MasterSeed;
            var depthLimit = (uint)_options.Value.DepthLimit;

            var objects = new List <object>();

            var type = _finder.GetTypeFromAssemblies(typeName);

            var generator = _generatorFactory.CreateObjectGenerator(type, masterSeed, depthLimit);

            var generatorType = generator.GetType();

            dynamic dynamicGenerator = Convert.ChangeType(generator, generatorType);

            dynamicGenerator = dynamicGenerator.Skip(skip);

            for (int i = 0; i < take; i++)
            {
                objects.Add((object)dynamicGenerator.GetNext());
            }

            return(objects);
        }