private void ResolveProperties <T>(T newlyCreatedObject) { foreach (PropertyInfo prop in newlyCreatedObject.GetType().GetProperties() .Where(x => x.GetCustomAttributes(typeof(DependencyConstructorAttribute), false).Count() > 0)) { IFactoryProvider factoryProvider = components.Single(x => x.Key.TypeToCreate == prop.PropertyType || prop.PropertyType.IsAssignableFrom(x.Key.TypeToLookFor)).Value; if (factoryProvider != null) { prop.SetValue(newlyCreatedObject, factoryProvider.Create(), null); } else { throw new Exception(string.Format( "Couldn't find instance of {0} to use for property injection", prop.PropertyType.FullName)); } } }
public T CreateInstance <T>() { lock (syncLock) { IFactoryProvider creator = components. Where(x => x.Key.TypeToCreate == typeof(T)). Select(x => x.Value).SingleOrDefault(); if (creator != null) { T newlyCreatedObject = (T)creator.Create(); SatisfyProperties <T>(newlyCreatedObject); return(newlyCreatedObject); } throw new ContainerConfigurationException(string.Format( "Couldn't create instance of {0} could not find correct " + "IFactoryProvider. This may be down to missing Component registration", typeof(T).FullName)); } }