我发现这个实现NBuilder的位置: https://gist.github.com/markgibaud/4150878NBuilder用法在收藏
这就像一个魅力,直到我需要里面添加一些收藏。
例如:
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;}
//...
}
我想,以填补任何收集与至少一个记录。
我结束了这段代码:
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;
}
}
但是还有一些问题也当我尝试调用构建方法。
您的代码是正确的,并返回传递的类型列表。我认为你的问题源于你如何设法分配这种方法的结果。请添加有关您如何使用它的信息。 – samy 2014-10-15 11:26:30
下面是这个例子:http://pastebin.com/pp9HQ6ci – ShP 2014-10-15 13:42:43
ShP,你可以添加最少的代码来重现问题到你的问题,并添加你遇到的错误吗? – samy 2014-10-15 13:45:18