/// <summary> /// /// </summary> /// <param name="inheritance"></param> /// <returns></returns> public T GenerateItem(Int32 inheritance = 2) { T result = (T)Activator.CreateInstance(typeof(T)); if (inheritance > 0) { inheritance--; foreach (var item in itemProperties) { PropertyInfo property = typeof(T).GetProperty(item.Key); if (property.CanWrite && property.GetSetMethod(/*nonPublic*/ true).IsPublic) { if (property.CustomAttributes.Where(x => x.AttributeType.Name.Equals("FakerTyper")).ToList().Count > 0) { foreach (var item1 in property.CustomAttributes) { TypeEnumCustom tItem = (TypeEnumCustom)Convert.ToInt32(item1.ConstructorArguments[0].Value.ToString()); switch (tItem) { case TypeEnumCustom.USERFIRSTNAME: property.SetValue(result, Faker.Name.First()); break; case TypeEnumCustom.USERLASTNAME: property.SetValue(result, Faker.Name.Last()); break; case TypeEnumCustom.LOREUMONEWORD: property.SetValue(result, Faker.Lorem.GetFirstWord()); break; case TypeEnumCustom.ADDRESSCITY: property.SetValue(result, Faker.Address.City()); break; case TypeEnumCustom.ADDRESSNUMBER: property.SetValue(result, Faker.RandomNumber.Next(1, 100).ToString()); break; case TypeEnumCustom.ADDRESSPOSTALCODE: property.SetValue(result, Faker.Address.UkPostCode()); break; case TypeEnumCustom.ADDRESSWAY: property.SetValue(result, Faker.Address.StreetName()); break; default: break; } } } else { String toSwitchOn = ""; if (property.PropertyType.Name == TypeEnum.NULLABLE) { toSwitchOn = property.PropertyType.GenericTypeArguments[0].Name; } else { toSwitchOn = property.PropertyType.Name; } switch (toSwitchOn) { case TypeEnum.INT32: property.SetValue(result, Faker.RandomNumber.Next(Int32.MaxValue)); break; case TypeEnum.INT: property.SetValue(result, Faker.RandomNumber.Next(Int32.MaxValue)); break; case TypeEnum.STRING: property.SetValue(result, Faker.Name.FullName()); break; case TypeEnum.LIST: object list = Activator.CreateInstance( typeof(List <>).MakeGenericType(property.PropertyType.GetGenericArguments())); property.SetValue(result, list); break; case TypeEnum.NULLABLE: break; default: object generator = Activator.CreateInstance(typeof(EntityGeneratorFakerTyper <>) .MakeGenericType(new Type[] { property.PropertyType })); property.SetValue(result, generator.GetType().GetMethod("GenerateItem").Invoke(generator, new object[] { inheritance })); break; } } } } } return(result); }
public FakerTyper(TypeEnumCustom type) { this.Typer = type; }