Пример #1
0
        private void FillObject(object instance)
        {
            var type = instance.GetType();

            var members = new List <MemberInfo>(type.GetMembers());

            foreach (var member in members)
            {
                if (member.MemberType == MemberTypes.Field)
                {
                    var fieldInfo = member as FieldInfo;
                    if (fieldInfo == null)
                    {
                        continue;
                    }
                    if (fieldInfo.IsLiteral)
                    {
                        continue;
                    }
                    var generator = _config.GetGeneratorByMemberInfo(fieldInfo);
                    if (generator != null)
                    {
                        fieldInfo.SetValue(instance, generator.Generate());
                    }
                    else
                    {
                        var fieldType = fieldInfo.FieldType;
                        var value     = Create(fieldType);
                        fieldInfo.SetValue(instance, value);
                    }
                }
                else if (member.MemberType == MemberTypes.Property)
                {
                    var propertyInfo = member as PropertyInfo;
                    if (propertyInfo == null)
                    {
                        continue;
                    }
                    if (!propertyInfo.CanWrite)
                    {
                        continue;
                    }
                    var generator = _config.GetGeneratorByMemberInfo(propertyInfo);
                    if (generator != null)
                    {
                        propertyInfo.SetValue(instance, generator.Generate());
                    }
                    else
                    {
                        var propertyType = propertyInfo.PropertyType;
                        var value        = Create(propertyType);
                        propertyInfo.SetValue(instance, value);
                    }
                }
            }
        }
Пример #2
0
        private object InitConstructor(IEnumerable <ConstructorInfo> constructors)
        {
            object obj = null;

            foreach (var constructor in constructors)
            {
                if (constructor.IsPrivate)
                {
                    continue;
                }

                try
                {
                    var list = new List <object>();

                    for (var i = 0; i < constructor.GetParameters().Length; i++)
                    {
                        var paramType = constructor.GetParameters()[i].ParameterType;

                        var generator = _config.GetGeneratorByMemberInfo(constructor.GetParameters()[i].Member);

                        if (generator != null)
                        {
                            list.Add(generator.Generate(new GeneratorContext(this, paramType)));
                        }
                        else
                        {
                            list.Add(Create(paramType));
                        }
                    }
                    obj = constructor.Invoke(list.ToArray());
                    break;
                }
                catch (Exception)
                {
                    // перебор конструкторов, всё хорошо =)
                }
            }

            if (obj == null)
            {
                throw  new Exception("Faker can't crate object of class " + obj.GetType().Name);
            }
            return(obj);
        }