//Create all the factories based on their types private List <DiscoverableFactory> InstantiateFactories(List <Type> factoryTypes) { List <DiscoverableFactory> factories = new List <DiscoverableFactory>(); foreach (Type t in factoryTypes) { DiscoverableFactory factory = (DiscoverableFactory)Activator.CreateInstance(t); factories.Add(factory); } return(factories); }
private static object MakeItem(Type consumedType, ConvenienceStressState state, DeterministicRandom random, int recursionDepth) { //Look up a Factory supporting this type DiscoverableFactory factory = state.GetFactory(consumedType, random, FavorSimpleFactories(recursionDepth)); //Provide the factory with what it needs PopulateDiscoverableInputs(factory, random, state, recursionDepth); //Create desired type via factory return(factory.Create(consumedType, random)); }
//Register all the factories supporting inputs to their consumed type. Fail if no factory supports a demanded input. private void MapFactories(List <DiscoverableFactory> factories, List <Type> inputTypes) { List <Type> unsupportedOutputs = new List <Type>(); Trace.WriteLine("[FactoryPool] Checking for satisfaction of inputs."); foreach (Type inputType in inputTypes) { Type testedType = inputType; bool inputMatchFound = false; // if (inputType.IsGenericType) { if (testedType.GetGenericTypeDefinition() == typeof(List <>)) { testedType = testedType.GetGenericArguments()[0]; } else { throw new InvalidOperationException("Unsupported inputType:" + inputType); } } Trace.WriteLine("[FactoryPool] Checking producers of " + inputType); //Find all the matching factories for a given type for (int factoryIndex = 0; factoryIndex < factories.Count; factoryIndex++) { DiscoverableFactory factory = factories[factoryIndex]; if (factory.CanCreate(testedType)) { Trace.WriteLine("[FactoryPool] Matched on Factory:" + factory.GetType()); inputMatchFound = true; factoryDictionary.Add(testedType, factory.GetType()); } } if (!inputMatchFound) { unsupportedOutputs.Add(testedType); } } if (unsupportedOutputs.Count > 0) { throw new ArgumentException(String.Format("These requested inputTypes could not be fulfilled by Factory pool:{0}", DumpList(unsupportedOutputs))); } }