我發現這個實現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