public XmlSerializer CreateSerializer(Type type, string defaultNamespace)
{
if (type == null)
throw new ArgumentNullException(nameof(type));
TempAssembly tempAssembly = s_cache[defaultNamespace, type];
XmlTypeMapping mapping = null;
if (tempAssembly == null)
{
lock (s_cache)
{
tempAssembly = s_cache[defaultNamespace, type];
if (tempAssembly == null)
{
XmlSerializerImplementation contract;
Assembly assembly = TempAssembly.LoadGeneratedAssembly(type, defaultNamespace, out contract);
if (assembly == null)
{
// need to reflect and generate new serialization assembly
XmlReflectionImporter importer = new XmlReflectionImporter(defaultNamespace);
mapping = importer.ImportTypeMapping(type, null, defaultNamespace);
tempAssembly = XmlSerializer.GenerateTempAssembly(mapping, type, defaultNamespace);
}
else
{
tempAssembly = new TempAssembly(contract);
}
s_cache.Add(defaultNamespace, type, tempAssembly);
}
}
}
if (mapping == null)
{
mapping = XmlReflectionImporter.GetTopLevelMapping(type, defaultNamespace);
}
return (XmlSerializer)tempAssembly.Contract.GetSerializer(type);
}