private FactoryRecord FindAndEnsureFactoryRecord(string configKey, out bool isRootDeclaredHere) {
isRootDeclaredHere = false;
BaseConfigurationRecord configRecord;
FactoryRecord factoryRecord = FindFactoryRecord(configKey, false, out configRecord);
if (factoryRecord != null && !factoryRecord.IsGroup) {
//
// Find the root declaration
//
FactoryRecord rootFactoryRecord = factoryRecord;
BaseConfigurationRecord rootConfigRecord = configRecord;
BaseConfigurationRecord currentConfigRecord = configRecord._parent;
while (!currentConfigRecord.IsRootConfig) {
BaseConfigurationRecord tempConfigRecord;
FactoryRecord tempFactoryRecord = currentConfigRecord.FindFactoryRecord(configKey, false, out tempConfigRecord);
if (tempFactoryRecord == null)
break;
rootFactoryRecord = tempFactoryRecord;
rootConfigRecord = tempConfigRecord;
// continue the search from the parent of the configRecord we found
currentConfigRecord = tempConfigRecord.Parent;
}
//
// A child factory record must be equivalent to its parent,
// so if the child has no errors, the parent must also have no errors.
//
Debug.Assert(!rootFactoryRecord.HasErrors, "!rootFactoryRecord.HasErrors");
if (rootFactoryRecord.Factory == null) {
try {
//
// Create the factory from the type string, and cache it
//
object factory = rootConfigRecord.CreateSectionFactory(rootFactoryRecord);
bool isFactoryTrustedWithoutAptca = TypeUtil.IsTypeFromTrustedAssemblyWithoutAptca(factory.GetType());
rootFactoryRecord.Factory = factory;
rootFactoryRecord.IsFactoryTrustedWithoutAptca = isFactoryTrustedWithoutAptca;
}
catch (Exception e) {
throw ExceptionUtil.WrapAsConfigException(SR.GetString(SR.Config_exception_creating_section_handler, factoryRecord.ConfigKey), e, factoryRecord);
}
catch {
throw ExceptionUtil.WrapAsConfigException(SR.GetString(SR.Config_exception_creating_section_handler, factoryRecord.ConfigKey), null, factoryRecord);
}
}
if (factoryRecord.Factory == null) {
factoryRecord.Factory = rootFactoryRecord.Factory;
factoryRecord.IsFactoryTrustedWithoutAptca = rootFactoryRecord.IsFactoryTrustedWithoutAptca;
}
isRootDeclaredHere = Object.ReferenceEquals(this, rootConfigRecord);
}
return factoryRecord;
}