I’m using nBuilder to generate some Testdata for my application.
First I tested it and it worked fine.
An easy Example:
Customer customer = Builder<Customer>
.CreateNew()
.Build();
Creates a Object and fills all Properties automatically.
By Example, if customer Contains the attribute: name, it will fill it with name1
and so on…
Well all this works fine, but I have troubles to do that whole thing dynamically now.
What I’m doing now, is Reflection, I’m iterating through all Entities in my Class and foreach of them there should be generated some Testdata, even lookups and childlists should be filled, but thats not a problem.. My question is, how I’m using the above code with any type?
ANYTYPE object = Builder<ANYTYPE> ...
What I tried:
object entity = null; //The object/Entity
Assembly assembly = Assembly.GetAssembly(typeof(EMI_ERPContext)); //Getting Assembly
Type type = assembly.GetType(entityName); //I know the Type
//entity = Activator.CreateInstance(type); Do I must create an Instance here?
object entity = Builder<dynamic> //The above code.. Tried to put dynamic as Type, but doesnt work
.CreateNew()
.Build();
I tested with a console app (complete here), faking the classes / interfaces / methods of nBuilder.
So this works, but not tried in real context.
The method you could reuse is “TryToReflectBuilder”. It could be much less verbose, but I let the “Step by step” code, as it’s probably more explicit. ReflectionConsole.Test is used as the “entity to reflect”.