private RandomValueViaFunction <object> NewListFactoryFor(Type type) { //perform a bunch of precalc Type itemType = type.GetGenericArguments()[0]; Type concreteListType; if (type.IsInterface) { concreteListType = typeof(List <>).MakeGenericType(type.GetGenericArguments()); } else { concreteListType = type; } var concreteListCtor = concreteListType.GetConstructor(new Type[] {}); var factory = new RandomValueViaFunction <Object>((parentType, listPropertyName, listType) => { dynamic list = concreteListCtor.Invoke(new object[] {}); var numItems = m_random.Int(MinColSize, MaxColSize + 1); //don't use the list count as we may be using a static key dependening on the user generator //and the lis may only allow uniques for (int i = 0; i < numItems; i++) { dynamic item = InternalNewRandomValueFor(parentType, listPropertyName + ".Value", itemType); list.Add(item); } return(list); }); return(factory); }
private void SetValueFactoryFor <TPropertyType>(Func <Type, TPropertyType> func) { var type = typeof(TPropertyType); var generator = new RandomValueViaFunction <TPropertyType>(func); SetValueFactoryFor(type, generator); }
public Builder ValueFactoryForType <TPropertyType>(Func <TPropertyType> factoryFunction) { var type = typeof(TPropertyType); var factory = new RandomValueViaFunction <TPropertyType>(factoryFunction); ValueFactoryForType(type, factory); return(this); }
private RandomValueViaFunction <object> NewInterfaceFactoryFor(Type onInstanceType, string propertyName, Type type) { var factory = new RandomValueViaFunction <Object>((genType) => { var propertyInstance = NewInstanceFromInterface(onInstanceType, propertyName, type); InternalFillWithRandom(propertyInstance); return(propertyInstance); }); return(factory); }
private RandomValueViaFunction <object> NewArrayFactory() { var factory = new RandomValueViaFunction <Object>((parentType, listPropertyName, arrayType) => { Type itemType = arrayType.GetElementType(); var numItems = m_random.Int(MinColSize, MaxColSize + 1); dynamic array = Array.CreateInstance(itemType, numItems); for (int i = 0; i < numItems; i++) { dynamic item = InternalNewRandomValueFor(parentType, listPropertyName + ".Value", itemType); array[i] = item; } return(array); }); return(factory); }
private RandomValueViaFunction <object> NewClassInstanceFactory() { var factory = new RandomValueViaFunction <Object>((genType) => { Object propertyInstance; try { propertyInstance = Activator.CreateInstance(genType); } catch (Exception e) { throw new RandomFillException("Error creating new instance for type:" + genType.PrettyName(), e); } InternalFillWithRandom(propertyInstance); return(propertyInstance); }); return(factory); }
private RandomValueViaFunction <object> NewDictionaryFactoryFor(Type type) { //perform a bunch of precalc Type keyType = type.GetGenericArguments()[0]; Type valueType = type.GetGenericArguments()[1]; Type concreteDictType; if (type.IsInterface) { concreteDictType = typeof(Dictionary <,>).MakeGenericType(type.GetGenericArguments()); } else { concreteDictType = type; } var concreteDictCtor = concreteDictType.GetConstructor(new Type[] {}); var factory = new RandomValueViaFunction <Object>((parentType, dictPropertyName, dictType) => { dynamic dict = concreteDictCtor.Invoke(new object[] {}); var numItems = m_random.Int(MinColSize, MaxColSize + 1); //don't use the dicationary count as we may be using a static key dependening on the user generator for (int i = 0; i < numItems; i++) { //todo:allow customisation or keys? dynamic key = InternalNewRandomValueFor(parentType, dictPropertyName + ".Key", keyType); dynamic val = InternalNewRandomValueFor(parentType, dictPropertyName + ".Value", valueType); if (!dict.ContainsKey(key)) { dict.Add(key, val); } } return(dict); }); return(factory); }