I found this implementation of NBuilder here: https://gist.github.com/markgibaud/4150878
This works like a charm until I need to add some collection inside.
For example:
public class UserDto
{
public string UserName {get;set;}
public List<AddressDto> Addresses {get;set;} //this will be null
}
public class AddressDto
{
public string Street {get;set;}
//...
}
I want to fill any collection with at least one record.
I end up with this piece of code:
private static object BuildObjectList(Type type)
{
try
{
var builderClassType = typeof (Builder<>);
Type[] args = {type};
var genericBuilderType = builderClassType.MakeGenericType(args);
var builder = Activator.CreateInstance(genericBuilderType);
var createListOfSizeMethodInfo = builder.GetType().GetMethod("CreateListOfSize", new[] {typeof (int)});
var objectBuilder = createListOfSizeMethodInfo.Invoke(builder, new object[] {1});
var buildMethodInfo = objectBuilder.GetType().GetMethod("Build");
return buildMethodInfo.Invoke(objectBuilder, null);
}
catch (Exception ex)
{
Console.WriteLine(ex);
return null;
}
}
But there's some issue also when I try to invoke build method.