/// <summary> /// Register a type that can be generated in this seed data /// </summary> private static void RegisterGeneratableType(Type type) { GeneratableType generatableType = new GeneratableType { Type = type }; PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo propertyInfo in properties) { if (!DummyDataProperties.Contains($"{propertyInfo.Name}s")) { continue; } generatableType.PropertyInfos.Add(propertyInfo); } AvailableEventTypes.Add(generatableType); }
/// <summary> /// Generate a random event type with populated data /// </summary> public static DomainEvent GenerateRandomEvent() { if (!AvailableEventTypes.Any()) { throw new ArgumentException("No available event types found to generate"); } GeneratableType type = AvailableEventTypes[Random.Next(0, AvailableEventTypes.Count)]; object instance = Activator.CreateInstance(type.Type); foreach (PropertyInfo propertyInfo in type.PropertyInfos) { var dummyDataProperty = SeedDataType.GetField($"{propertyInfo.Name}s") ?.GetValue(null) as object[]; propertyInfo.SetValue(instance, dummyDataProperty?[Random.Next(dummyDataProperty.Length)]); } return(instance as DomainEvent); }