Search code examples
c#nbuilder

Getting NBuilder to build a List of Lists


I'm getting a parameter count mismatch with NBuilder, basically trying to Build up a List of Lists, can't seem to find any examples using NBuilder to do this:

public class MyClass
{
   public IEnumerable<IEnumerable<int>> Matrix { get; set; }
}

_myClass.Matrix = Builder<List<int>>.CreateListOfSize(10).Build();

System.Reflection.TargetParameterCountException : Parameter count mismatch.

Solution

  • You can't create list of lists with NBuilder - it fails to initialize properties of generic type parameter, which is List<int> in your case. NBuilder finds indexer property on list and tries to initialize it's value this way (you can find source code here):

    propertyInfo.SetValue(obj, value, null);
    

    But for indexer property index should be passed in last parameter. That's causes exception Parameter count mismatch.


    Here is how you can initialize matrix:

    _myClass.Matrix = Enumerable.Repeat(Enumerable.Range(0, 100).ToList(), 100);
    

    Or with NBuilder

    _myClass.Matrix = Enumerable.Repeat(Builder<int>.CreateListOfSize(100).Build(), 100);