public static MockProjectElementLinkRemoter ExportElement(this ProjectCollectionLinker exporter, ProjectElement xml) { if (xml == null) { return(null); } var implType = xml.GetType(); if (knownTypes.TryGetValue(implType, out var factory)) { return(factory(exporter, xml)); } factory = NotImplemented; foreach (var t in canonicalTypes) { if (t.Checker(xml)) { factory = t.ExportFactory; break; } } lock (knownTypes) { var newKnown = new Dictionary <Type, ExporterFactory>(knownTypes); newKnown[implType] = factory; knownTypes = newKnown; } return(factory(exporter, xml)); }