private object CreateAndPopulate(
Type type,
string referenceName,
LinkedList<object> buildChain,
object[] args,
ITypeCreator typeCreator)
{
var instance = CreateInstance(typeCreator, type, referenceName, buildChain, args);
if (instance == null)
{
return null;
}
try
{
_buildChain.Push(instance);
if (typeCreator.AutoPopulate)
{
// The type creator has indicated that this type should be auto populated by the execute strategy
instance = PopulateInstance(instance);
Debug.Assert(instance != null, "Populating the instance did not return the original instance");
}
// Allow the type creator to do its own population of the instance
instance = typeCreator.Populate(instance, this);
var postBuildActions =
Configuration.PostBuildActions?.Where(x => x.IsSupported(type, referenceName, BuildChain))
.OrderByDescending(x => x.Priority);
if (postBuildActions != null)
{
foreach (var postBuildAction in postBuildActions)
{
postBuildAction.Execute(type, referenceName, BuildChain);
}
}
return instance;
}
finally
{
_buildChain.Pop();
}
}