XmlSerializer.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. //
  2. // XmlSerializer.cs:
  3. //
  4. // Author:
  5. // Lluis Sanchez Gual ([email protected])
  6. //
  7. // (C) 2002, 2003 Ximian, Inc. http://www.ximian.com
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Threading;
  31. using System.Collections;
  32. using System.Globalization;
  33. using System.IO;
  34. using System.Reflection;
  35. using System.Xml;
  36. using System.Xml.Schema;
  37. using System.Text;
  38. #if !TARGET_JVM
  39. using System.CodeDom;
  40. using System.CodeDom.Compiler;
  41. using Microsoft.CSharp;
  42. #endif
  43. using System.Configuration;
  44. using System.Security.Policy;
  45. namespace System.Xml.Serialization
  46. {
  47. public class XmlSerializer
  48. {
  49. internal const string WsdlNamespace = "http://schemas.xmlsoap.org/wsdl/";
  50. internal const string EncodingNamespace = "http://schemas.xmlsoap.org/soap/encoding/";
  51. static int generationThreshold;
  52. static bool backgroundGeneration = true;
  53. static bool deleteTempFiles = true;
  54. bool customSerializer;
  55. XmlMapping typeMapping;
  56. SerializerData serializerData;
  57. static Hashtable serializerTypes = new Hashtable ();
  58. internal class SerializerData
  59. {
  60. public int UsageCount;
  61. public Type ReaderType;
  62. public MethodInfo ReaderMethod;
  63. public Type WriterType;
  64. public MethodInfo WriterMethod;
  65. public GenerationBatch Batch;
  66. public IXmlSerializerImplementation Implementation = null;
  67. public XmlSerializationReader CreateReader () {
  68. if (ReaderType != null)
  69. return (XmlSerializationReader) Activator.CreateInstance (ReaderType);
  70. else if (Implementation != null)
  71. return Implementation.Reader;
  72. else
  73. return null;
  74. }
  75. public XmlSerializationWriter CreateWriter () {
  76. if (WriterType != null)
  77. return (XmlSerializationWriter) Activator.CreateInstance (WriterType);
  78. else if (Implementation != null)
  79. return Implementation.Writer;
  80. else
  81. return null;
  82. }
  83. }
  84. internal class GenerationBatch
  85. {
  86. public bool Done;
  87. public XmlMapping[] Maps;
  88. public SerializerData[] Datas;
  89. }
  90. static XmlSerializer ()
  91. {
  92. #if TARGET_JVM
  93. string db = null;
  94. string th = null;
  95. generationThreshold = -1;
  96. backgroundGeneration = false;
  97. #else
  98. string db = Environment.GetEnvironmentVariable ("MONO_XMLSERIALIZER_DEBUG");
  99. string th = Environment.GetEnvironmentVariable ("MONO_XMLSERIALIZER_THS");
  100. if (th == null) {
  101. generationThreshold = 50;
  102. backgroundGeneration = true;
  103. }
  104. else if (th.ToLower(CultureInfo.InvariantCulture) == "no")
  105. generationThreshold = -1;
  106. else {
  107. generationThreshold = int.Parse (th, CultureInfo.InvariantCulture);
  108. backgroundGeneration = (generationThreshold != 0);
  109. if (generationThreshold < 1) generationThreshold = 1;
  110. }
  111. #endif
  112. deleteTempFiles = (db == null || db == "no");
  113. IDictionary table = (IDictionary) ConfigurationSettings.GetConfig("system.diagnostics");
  114. if (table != null)
  115. {
  116. table = (IDictionary) table["switches"];
  117. if (table != null)
  118. {
  119. string val = (string) table ["XmlSerialization.Compilation"];
  120. if (val == "1") deleteTempFiles = false;
  121. }
  122. }
  123. }
  124. #region Constructors
  125. protected XmlSerializer ()
  126. {
  127. customSerializer = true;
  128. }
  129. public XmlSerializer (Type type)
  130. : this (type, null, null, null, null)
  131. {
  132. }
  133. public XmlSerializer (XmlTypeMapping xmlTypeMapping)
  134. {
  135. typeMapping = xmlTypeMapping;
  136. }
  137. internal XmlSerializer (XmlMapping mapping, SerializerData data)
  138. {
  139. typeMapping = mapping;
  140. serializerData = data;
  141. }
  142. public XmlSerializer (Type type, string defaultNamespace)
  143. : this (type, null, null, null, defaultNamespace)
  144. {
  145. }
  146. public XmlSerializer (Type type, Type[] extraTypes)
  147. : this (type, null, extraTypes, null, null)
  148. {
  149. }
  150. public XmlSerializer (Type type, XmlAttributeOverrides overrides)
  151. : this (type, overrides, null, null, null)
  152. {
  153. }
  154. public XmlSerializer (Type type, XmlRootAttribute root)
  155. : this (type, null, null, root, null)
  156. {
  157. }
  158. public XmlSerializer (Type type,
  159. XmlAttributeOverrides overrides,
  160. Type [] extraTypes,
  161. XmlRootAttribute root,
  162. string defaultNamespace)
  163. {
  164. if (type == null)
  165. throw new ArgumentNullException ("type");
  166. XmlReflectionImporter importer = new XmlReflectionImporter (overrides, defaultNamespace);
  167. if (extraTypes != null)
  168. {
  169. foreach (Type intype in extraTypes)
  170. importer.IncludeType (intype);
  171. }
  172. typeMapping = importer.ImportTypeMapping (type, root, defaultNamespace);
  173. }
  174. internal XmlMapping Mapping
  175. {
  176. get { return typeMapping; }
  177. }
  178. #if NET_2_0
  179. [MonoTODO]
  180. public XmlSerializer (Type type,
  181. XmlAttributeOverrides overrides,
  182. Type [] extraTypes,
  183. XmlRootAttribute root,
  184. string defaultNamespace,
  185. string location,
  186. Evidence evidence)
  187. {
  188. }
  189. #endif
  190. #endregion // Constructors
  191. #region Events
  192. private XmlAttributeEventHandler onUnknownAttribute;
  193. private XmlElementEventHandler onUnknownElement;
  194. private XmlNodeEventHandler onUnknownNode;
  195. private UnreferencedObjectEventHandler onUnreferencedObject;
  196. public event XmlAttributeEventHandler UnknownAttribute
  197. {
  198. add { onUnknownAttribute += value; } remove { onUnknownAttribute -= value; }
  199. }
  200. public event XmlElementEventHandler UnknownElement
  201. {
  202. add { onUnknownElement += value; } remove { onUnknownElement -= value; }
  203. }
  204. public event XmlNodeEventHandler UnknownNode
  205. {
  206. add { onUnknownNode += value; } remove { onUnknownNode -= value; }
  207. }
  208. public event UnreferencedObjectEventHandler UnreferencedObject
  209. {
  210. add { onUnreferencedObject += value; } remove { onUnreferencedObject -= value; }
  211. }
  212. internal virtual void OnUnknownAttribute (XmlAttributeEventArgs e)
  213. {
  214. if (onUnknownAttribute != null) onUnknownAttribute(this, e);
  215. }
  216. internal virtual void OnUnknownElement (XmlElementEventArgs e)
  217. {
  218. if (onUnknownElement != null) onUnknownElement(this, e);
  219. }
  220. internal virtual void OnUnknownNode (XmlNodeEventArgs e)
  221. {
  222. if (onUnknownNode != null) onUnknownNode(this, e);
  223. }
  224. internal virtual void OnUnreferencedObject (UnreferencedObjectEventArgs e)
  225. {
  226. if (onUnreferencedObject != null) onUnreferencedObject(this, e);
  227. }
  228. #endregion // Events
  229. #region Methods
  230. public virtual bool CanDeserialize (XmlReader xmlReader)
  231. {
  232. xmlReader.MoveToContent ();
  233. if (typeMapping is XmlMembersMapping)
  234. return true;
  235. else
  236. return ((XmlTypeMapping)typeMapping).ElementName == xmlReader.LocalName;
  237. }
  238. protected virtual XmlSerializationReader CreateReader ()
  239. {
  240. // Must be implemented in derived class
  241. throw new NotImplementedException ();
  242. }
  243. protected virtual XmlSerializationWriter CreateWriter ()
  244. {
  245. // Must be implemented in derived class
  246. throw new NotImplementedException ();
  247. }
  248. public object Deserialize (Stream stream)
  249. {
  250. XmlTextReader xmlReader = new XmlTextReader(stream);
  251. xmlReader.Normalization = true;
  252. return Deserialize(xmlReader);
  253. }
  254. public object Deserialize (TextReader textReader)
  255. {
  256. XmlTextReader xmlReader = new XmlTextReader(textReader);
  257. xmlReader.Normalization = true;
  258. return Deserialize(xmlReader);
  259. }
  260. public object Deserialize (XmlReader xmlReader)
  261. {
  262. XmlSerializationReader xsReader;
  263. if (customSerializer)
  264. xsReader = CreateReader ();
  265. else
  266. xsReader = CreateReader (typeMapping);
  267. xsReader.Initialize (xmlReader, this);
  268. return Deserialize (xsReader);
  269. }
  270. protected virtual object Deserialize (XmlSerializationReader reader)
  271. {
  272. if (customSerializer)
  273. // Must be implemented in derived class
  274. throw new NotImplementedException ();
  275. if (reader is XmlSerializationReaderInterpreter)
  276. return ((XmlSerializationReaderInterpreter)reader).ReadRoot ();
  277. else
  278. return serializerData.ReaderMethod.Invoke (reader, null);
  279. }
  280. public static XmlSerializer [] FromMappings (XmlMapping [] mappings)
  281. {
  282. XmlSerializer[] sers = new XmlSerializer [mappings.Length];
  283. SerializerData[] datas = new SerializerData [mappings.Length];
  284. GenerationBatch batch = new GenerationBatch ();
  285. batch.Maps = mappings;
  286. batch.Datas = datas;
  287. for (int n=0; n<mappings.Length; n++)
  288. {
  289. if (mappings[n] != null)
  290. {
  291. SerializerData data = new SerializerData ();
  292. data.Batch = batch;
  293. sers[n] = new XmlSerializer (mappings[n], data);
  294. datas[n] = data;
  295. }
  296. }
  297. return sers;
  298. }
  299. public static XmlSerializer [] FromTypes (Type [] mappings)
  300. {
  301. XmlSerializer [] sers = new XmlSerializer [mappings.Length];
  302. for (int n=0; n<mappings.Length; n++)
  303. sers[n] = new XmlSerializer (mappings[n]);
  304. return sers;
  305. }
  306. protected virtual void Serialize (object o, XmlSerializationWriter writer)
  307. {
  308. if (customSerializer)
  309. // Must be implemented in derived class
  310. throw new NotImplementedException ();
  311. if (writer is XmlSerializationWriterInterpreter)
  312. ((XmlSerializationWriterInterpreter)writer).WriteRoot (o);
  313. else
  314. serializerData.WriterMethod.Invoke (writer, new object[] {o});
  315. }
  316. public void Serialize (Stream stream, object o)
  317. {
  318. XmlTextWriter xmlWriter = new XmlTextWriter (stream, System.Text.Encoding.Default);
  319. xmlWriter.Formatting = Formatting.Indented;
  320. Serialize (xmlWriter, o, null);
  321. }
  322. public void Serialize (TextWriter textWriter, object o)
  323. {
  324. XmlTextWriter xmlWriter = new XmlTextWriter (textWriter);
  325. xmlWriter.Formatting = Formatting.Indented;
  326. Serialize (xmlWriter, o, null);
  327. }
  328. public void Serialize (XmlWriter xmlWriter, object o)
  329. {
  330. Serialize (xmlWriter, o, null);
  331. }
  332. public void Serialize (Stream stream, object o, XmlSerializerNamespaces namespaces)
  333. {
  334. XmlTextWriter xmlWriter = new XmlTextWriter (stream, System.Text.Encoding.Default);
  335. xmlWriter.Formatting = Formatting.Indented;
  336. Serialize (xmlWriter, o, namespaces);
  337. }
  338. public void Serialize (TextWriter textWriter, object o, XmlSerializerNamespaces namespaces)
  339. {
  340. XmlTextWriter xmlWriter = new XmlTextWriter (textWriter);
  341. xmlWriter.Formatting = Formatting.Indented;
  342. Serialize (xmlWriter, o, namespaces);
  343. xmlWriter.Flush();
  344. }
  345. public void Serialize (XmlWriter writer, object o, XmlSerializerNamespaces namespaces)
  346. {
  347. XmlSerializationWriter xsWriter;
  348. if (customSerializer)
  349. xsWriter = CreateWriter ();
  350. else
  351. xsWriter = CreateWriter (typeMapping);
  352. if (namespaces == null || namespaces.Count == 0)
  353. {
  354. namespaces = new XmlSerializerNamespaces ();
  355. namespaces.Add ("xsd", XmlSchema.Namespace);
  356. namespaces.Add ("xsi", XmlSchema.InstanceNamespace);
  357. }
  358. xsWriter.Initialize (writer, namespaces);
  359. Serialize (o, xsWriter);
  360. writer.Flush ();
  361. }
  362. #if NET_2_0
  363. [MonoTODO]
  364. public object Deserialize (XmlReader xmlReader, string encodingStyle, XmlDeserializationEvents events)
  365. {
  366. throw new NotImplementedException ();
  367. }
  368. [MonoTODO]
  369. public object Deserialize (XmlReader xmlReader, string encodingStyle)
  370. {
  371. throw new NotImplementedException ();
  372. }
  373. [MonoTODO]
  374. public object Deserialize (XmlReader xmlReader, XmlDeserializationEvents events)
  375. {
  376. throw new NotImplementedException ();
  377. }
  378. [MonoTODO]
  379. public static XmlSerializer[] FromMappings (XmlMapping[] mappings, Evidence evidence)
  380. {
  381. throw new NotImplementedException ();
  382. }
  383. [MonoTODO]
  384. public static XmlSerializer[] FromMappings (XmlMapping[] mappings, Type type)
  385. {
  386. throw new NotImplementedException ();
  387. }
  388. public static Assembly GenerateSerializer (Type[] types, XmlMapping[] mappings)
  389. {
  390. return GenerateSerializer (types, mappings, null);
  391. }
  392. [MonoTODO]
  393. public static Assembly GenerateSerializer (Type[] types, XmlMapping[] mappings, CompilerParameters parameters)
  394. {
  395. GenerationBatch batch = new GenerationBatch ();
  396. batch.Maps = mappings;
  397. batch.Datas = new SerializerData [mappings.Length];
  398. for (int n=0; n<mappings.Length; n++) {
  399. SerializerData data = new SerializerData ();
  400. data.Batch = batch;
  401. batch.Datas [n] = data;
  402. }
  403. return GenerateSerializers (batch, parameters);
  404. }
  405. [MonoTODO]
  406. [Obsolete]
  407. public static Assembly GenerateSerializer (Type[] types,
  408. XmlMapping[] mappings,
  409. string codePath,
  410. bool debug,
  411. bool keepFiles)
  412. {
  413. throw new NotImplementedException ();
  414. }
  415. [MonoTODO]
  416. [Obsolete]
  417. public static Assembly GenerateSerializer (Type[] types,
  418. XmlMapping[] mappings,
  419. string codePath,
  420. bool debug,
  421. bool keepFiles,
  422. string compilerOptions)
  423. {
  424. throw new NotImplementedException ();
  425. }
  426. public static string GetXmlSerializerAssemblyName (Type type)
  427. {
  428. return type.Assembly.GetName().Name + ".XmlSerializers";
  429. }
  430. public static string GetXmlSerializerAssemblyName (Type type, string defaultNamespace)
  431. {
  432. return GetXmlSerializerAssemblyName (type) + "." + defaultNamespace.GetHashCode ();
  433. }
  434. [MonoTODO]
  435. public void Serialize (XmlWriter xmlWriter, object o, XmlSerializerNamespaces namespaces, string encodingStyle)
  436. {
  437. throw new NotImplementedException ();
  438. }
  439. #endif
  440. XmlSerializationWriter CreateWriter (XmlMapping typeMapping)
  441. {
  442. XmlSerializationWriter writer;
  443. lock (this) {
  444. if (serializerData != null) {
  445. writer = serializerData.CreateWriter ();
  446. if (writer != null) return writer;
  447. }
  448. }
  449. if (!typeMapping.Source.CanBeGenerated || generationThreshold == -1)
  450. return new XmlSerializationWriterInterpreter (typeMapping);
  451. CheckGeneratedTypes (typeMapping);
  452. lock (this) {
  453. writer = serializerData.CreateWriter ();
  454. if (writer != null) return writer;
  455. }
  456. return new XmlSerializationWriterInterpreter (typeMapping);
  457. }
  458. XmlSerializationReader CreateReader (XmlMapping typeMapping)
  459. {
  460. XmlSerializationReader reader;
  461. lock (this) {
  462. if (serializerData != null) {
  463. reader = serializerData.CreateReader ();
  464. if (reader != null) return reader;
  465. }
  466. }
  467. if (!typeMapping.Source.CanBeGenerated || generationThreshold == -1)
  468. return new XmlSerializationReaderInterpreter (typeMapping);
  469. CheckGeneratedTypes (typeMapping);
  470. lock (this) {
  471. reader = serializerData.CreateReader ();
  472. if (reader != null) return reader;
  473. }
  474. return new XmlSerializationReaderInterpreter (typeMapping);
  475. }
  476. #if TARGET_JVM
  477. void CheckGeneratedTypes (XmlMapping typeMapping)
  478. {
  479. throw new NotImplementedException();
  480. }
  481. void GenerateSerializersAsync (GenerationBatch batch)
  482. {
  483. throw new NotImplementedException();
  484. }
  485. void RunSerializerGeneration (object obj)
  486. {
  487. throw new NotImplementedException();
  488. }
  489. #else
  490. void CheckGeneratedTypes (XmlMapping typeMapping)
  491. {
  492. lock (this)
  493. {
  494. if (serializerData == null)
  495. {
  496. lock (serializerTypes)
  497. {
  498. serializerData = (SerializerData) serializerTypes [typeMapping.Source];
  499. if (serializerData == null) {
  500. serializerData = new SerializerData();
  501. serializerTypes [typeMapping.Source] = serializerData;
  502. }
  503. }
  504. }
  505. }
  506. bool generate = false;
  507. lock (serializerData)
  508. {
  509. generate = (++serializerData.UsageCount == generationThreshold);
  510. }
  511. if (generate)
  512. {
  513. if (serializerData.Batch != null)
  514. GenerateSerializersAsync (serializerData.Batch);
  515. else
  516. {
  517. GenerationBatch batch = new GenerationBatch ();
  518. batch.Maps = new XmlMapping[] {typeMapping};
  519. batch.Datas = new SerializerData[] {serializerData};
  520. GenerateSerializersAsync (batch);
  521. }
  522. }
  523. }
  524. void GenerateSerializersAsync (GenerationBatch batch)
  525. {
  526. if (batch.Maps.Length != batch.Datas.Length)
  527. throw new ArgumentException ("batch");
  528. lock (batch)
  529. {
  530. if (batch.Done) return;
  531. batch.Done = true;
  532. }
  533. if (backgroundGeneration)
  534. ThreadPool.QueueUserWorkItem (new WaitCallback (RunSerializerGeneration), batch);
  535. else
  536. RunSerializerGeneration (batch);
  537. }
  538. void RunSerializerGeneration (object obj)
  539. {
  540. try
  541. {
  542. GenerationBatch batch = (GenerationBatch) obj;
  543. batch = LoadFromSatelliteAssembly (batch);
  544. if (batch != null)
  545. GenerateSerializers (batch, null);
  546. }
  547. catch (Exception ex)
  548. {
  549. Console.WriteLine (ex);
  550. }
  551. }
  552. static Assembly GenerateSerializers (GenerationBatch batch, CompilerParameters cp)
  553. {
  554. DateTime tim = DateTime.Now;
  555. XmlMapping[] maps = batch.Maps;
  556. if (cp == null) {
  557. cp = new CompilerParameters();
  558. cp.IncludeDebugInformation = false;
  559. cp.GenerateInMemory = true;
  560. cp.TempFiles.KeepFiles = !deleteTempFiles;
  561. }
  562. string file = cp.TempFiles.AddExtension ("cs");
  563. StreamWriter sw = new StreamWriter (file);
  564. if (!deleteTempFiles)
  565. Console.WriteLine ("Generating " + file);
  566. SerializationCodeGenerator gen = new SerializationCodeGenerator (maps);
  567. try
  568. {
  569. gen.GenerateSerializers (sw);
  570. }
  571. catch (Exception ex)
  572. {
  573. Console.WriteLine ("Serializer could not be generated");
  574. Console.WriteLine (ex);
  575. cp.TempFiles.Delete ();
  576. return null;
  577. }
  578. sw.Close ();
  579. CSharpCodeProvider provider = new CSharpCodeProvider();
  580. ICodeCompiler comp = provider.CreateCompiler ();
  581. cp.GenerateExecutable = false;
  582. foreach (Type rtype in gen.ReferencedTypes)
  583. {
  584. if (!cp.ReferencedAssemblies.Contains (rtype.Assembly.Location))
  585. cp.ReferencedAssemblies.Add (rtype.Assembly.Location);
  586. }
  587. if (!cp.ReferencedAssemblies.Contains ("System.dll"))
  588. cp.ReferencedAssemblies.Add ("System.dll");
  589. if (!cp.ReferencedAssemblies.Contains ("System.Xml"))
  590. cp.ReferencedAssemblies.Add ("System.Xml");
  591. if (!cp.ReferencedAssemblies.Contains ("System.Data"))
  592. cp.ReferencedAssemblies.Add ("System.Data");
  593. CompilerResults res = comp.CompileAssemblyFromFile (cp, file);
  594. if (res.Errors.HasErrors || res.CompiledAssembly == null) {
  595. Console.WriteLine ("Error while compiling generated serializer");
  596. foreach (CompilerError error in res.Errors)
  597. Console.WriteLine (error);
  598. cp.TempFiles.Delete ();
  599. return null;
  600. }
  601. GenerationResult[] results = gen.GenerationResults;
  602. for (int n=0; n<results.Length; n++)
  603. {
  604. GenerationResult gres = results[n];
  605. SerializerData sd = batch.Datas [n];
  606. lock (sd)
  607. {
  608. sd.WriterType = res.CompiledAssembly.GetType (gres.Namespace + "." + gres.WriterClassName);
  609. sd.ReaderType = res.CompiledAssembly.GetType (gres.Namespace + "." + gres.ReaderClassName);
  610. sd.WriterMethod = sd.WriterType.GetMethod (gres.WriteMethodName);
  611. sd.ReaderMethod = sd.ReaderType.GetMethod (gres.ReadMethodName);
  612. sd.Batch = null;
  613. }
  614. }
  615. cp.TempFiles.Delete ();
  616. if (!deleteTempFiles)
  617. Console.WriteLine ("Generation finished - " + (DateTime.Now - tim).TotalMilliseconds + " ms");
  618. return res.CompiledAssembly;
  619. }
  620. #endif
  621. #if NET_2_0
  622. GenerationBatch LoadFromSatelliteAssembly (GenerationBatch batch)
  623. {
  624. return batch;
  625. }
  626. #else
  627. GenerationBatch LoadFromSatelliteAssembly (GenerationBatch batch)
  628. {
  629. return batch;
  630. }
  631. #endif
  632. #endregion // Methods
  633. }
  634. }