예제 #1
0
        private bool TryCreateObject(FakerType fakerType, out object obj)
        {
            obj = null;
            ConstructorInfo[] constructors = fakerType.Type.GetConstructors(_flags);
            constructors = constructors.OrderByDescending(constructor => constructor.GetParameters().Length).
                           Append(typeof(object).GetConstructor(Type.EmptyTypes)).
                           ToArray();
            for (int i = 0; i < constructors.Length; i++)
            {
                ParameterInfo[] parameters        = constructors[i].GetParameters();
                object[]        constructorParams = new object[parameters.Length];
                for (int j = 0; j < parameters.Length; j++)
                {
                    constructorParams[j] = FillObject(new FakerType(parameters[j].ParameterType, fakerType.Type, parameters[j].Name));
                }

                try
                {
                    obj = Activator.CreateInstance(fakerType.Type, constructorParams);
                    break;
                }
                catch { }
            }
            return(obj != null);
        }
예제 #2
0
        public bool TryGenerateValue(FakerType fakerType, ObjectFiller objectFiller, out object obj)
        {
            obj = null;
            IGenerator generator;

            if (TryGetGenerator(fakerType, out generator))
            {
                obj = generator.GenerateValue(fakerType.Type, objectFiller);
            }
            return(obj != null);
        }
예제 #3
0
 private bool TryGetGenerator(FakerType fakerType, out IGenerator generator)
 {
     generator = null;
     if (Config == null || fakerType.BaseType == null)
     {
         _defaultGenerators.TryGetValue(fakerType.Type.MetadataToken, out generator);
     }
     else
     {
         if (!Config.TryGetGenerator(fakerType.BaseType, fakerType.MemberName, out generator))
         {
             _defaultGenerators.TryGetValue(fakerType.Type.MetadataToken, out generator);
         }
     }
     return(generator != null);
 }
예제 #4
0
        public object FillObject(FakerType fakerType)
        {
            object obj = null;

            if (!_generator.TryGenerateValue(fakerType, this, out obj) && fakerType.IsCreateAttribute)
            {
                if (TryCreateObject(fakerType, out obj))
                {
                    MemberInfo[] members = Metadata.GetFieldsAndProperties(fakerType.Type, _flags);
                    for (int i = 0; i < members.Length; i++)
                    {
                        object value = FillObject(new FakerType(Metadata.GetMemberType(members[i]), fakerType.Type, members[i].Name));
                        Metadata.SetValue(members[i], obj, value);
                    }
                }
            }
            return(obj);
        }