XmlSerializer.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  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. internal const string WsdlTypesNamespace = "http://microsoft.com/wsdl/types/";
  52. static int generationThreshold;
  53. static bool backgroundGeneration = true;
  54. static bool deleteTempFiles = true;
  55. static bool generatorFallback = true;
  56. bool customSerializer;
  57. XmlMapping typeMapping;
  58. SerializerData serializerData;
  59. static Hashtable serializerTypes = new Hashtable ();
  60. internal class SerializerData
  61. {
  62. public int UsageCount;
  63. public Type ReaderType;
  64. public MethodInfo ReaderMethod;
  65. public Type WriterType;
  66. public MethodInfo WriterMethod;
  67. public GenerationBatch Batch;
  68. public IXmlSerializerImplementation Implementation = null;
  69. public XmlSerializationReader CreateReader () {
  70. if (ReaderType != null)
  71. return (XmlSerializationReader) Activator.CreateInstance (ReaderType);
  72. else if (Implementation != null)
  73. return Implementation.Reader;
  74. else
  75. return null;
  76. }
  77. public XmlSerializationWriter CreateWriter () {
  78. if (WriterType != null)
  79. return (XmlSerializationWriter) Activator.CreateInstance (WriterType);
  80. else if (Implementation != null)
  81. return Implementation.Writer;
  82. else
  83. return null;
  84. }
  85. }
  86. internal class GenerationBatch
  87. {
  88. public bool Done;
  89. public XmlMapping[] Maps;
  90. public SerializerData[] Datas;
  91. }
  92. static XmlSerializer ()
  93. {
  94. // The following options are available:
  95. // MONO_XMLSERIALIZER_DEBUG: when set to something != "no", it will
  96. // it will print the name of the generated file, and it won't
  97. // be deleted.
  98. // MONO_XMLSERIALIZER_THS: The code generator threshold. It can be:
  99. // no: does not use the generator, always the interpreter.
  100. // 0: always use the generator, wait until the generation is done.
  101. // any number: use the interpreted serializer until the specified
  102. // number of serializations is reached. At this point the generation
  103. // of the serializer will start in the background. The interpreter
  104. // will be used while the serializer is being generated.
  105. //
  106. // XmlSerializer will fall back to the interpreted serializer if
  107. // the code generation somehow fails. This can be avoided for
  108. // debugging pourposes by adding the "nofallback" option.
  109. // For example: MONO_XMLSERIALIZER_THS=0,nofallback
  110. #if TARGET_JVM
  111. string db = null;
  112. string th = null;
  113. generationThreshold = -1;
  114. backgroundGeneration = false;
  115. #else
  116. string db = Environment.GetEnvironmentVariable ("MONO_XMLSERIALIZER_DEBUG");
  117. string th = Environment.GetEnvironmentVariable ("MONO_XMLSERIALIZER_THS");
  118. if (th == null) {
  119. generationThreshold = 50;
  120. backgroundGeneration = true;
  121. } else {
  122. int i = th.IndexOf (',');
  123. if (i != -1) {
  124. if (th.Substring (i+1) == "nofallback")
  125. generatorFallback = false;
  126. th = th.Substring (0, i);
  127. }
  128. if (th.ToLower(CultureInfo.InvariantCulture) == "no")
  129. generationThreshold = -1;
  130. else {
  131. generationThreshold = int.Parse (th, CultureInfo.InvariantCulture);
  132. backgroundGeneration = (generationThreshold != 0);
  133. if (generationThreshold < 1) generationThreshold = 1;
  134. }
  135. }
  136. #endif
  137. deleteTempFiles = (db == null || db == "no");
  138. IDictionary table = (IDictionary) ConfigurationSettings.GetConfig("system.diagnostics");
  139. if (table != null)
  140. {
  141. table = (IDictionary) table["switches"];
  142. if (table != null)
  143. {
  144. string val = (string) table ["XmlSerialization.Compilation"];
  145. if (val == "1") deleteTempFiles = false;
  146. }
  147. }
  148. }
  149. #region Constructors
  150. protected XmlSerializer ()
  151. {
  152. customSerializer = true;
  153. }
  154. public XmlSerializer (Type type)
  155. : this (type, null, null, null, null)
  156. {
  157. }
  158. public XmlSerializer (XmlTypeMapping xmlTypeMapping)
  159. {
  160. typeMapping = xmlTypeMapping;
  161. }
  162. internal XmlSerializer (XmlMapping mapping, SerializerData data)
  163. {
  164. typeMapping = mapping;
  165. serializerData = data;
  166. }
  167. public XmlSerializer (Type type, string defaultNamespace)
  168. : this (type, null, null, null, defaultNamespace)
  169. {
  170. }
  171. public XmlSerializer (Type type, Type[] extraTypes)
  172. : this (type, null, extraTypes, null, null)
  173. {
  174. }
  175. public XmlSerializer (Type type, XmlAttributeOverrides overrides)
  176. : this (type, overrides, null, null, null)
  177. {
  178. }
  179. public XmlSerializer (Type type, XmlRootAttribute root)
  180. : this (type, null, null, root, null)
  181. {
  182. }
  183. public XmlSerializer (Type type,
  184. XmlAttributeOverrides overrides,
  185. Type [] extraTypes,
  186. XmlRootAttribute root,
  187. string defaultNamespace)
  188. {
  189. if (type == null)
  190. throw new ArgumentNullException ("type");
  191. XmlReflectionImporter importer = new XmlReflectionImporter (overrides, defaultNamespace);
  192. if (extraTypes != null)
  193. {
  194. foreach (Type intype in extraTypes)
  195. importer.IncludeType (intype);
  196. }
  197. typeMapping = importer.ImportTypeMapping (type, root, defaultNamespace);
  198. }
  199. internal XmlMapping Mapping
  200. {
  201. get { return typeMapping; }
  202. }
  203. #if NET_2_0
  204. [MonoTODO]
  205. public XmlSerializer (Type type,
  206. XmlAttributeOverrides overrides,
  207. Type [] extraTypes,
  208. XmlRootAttribute root,
  209. string defaultNamespace,
  210. string location,
  211. Evidence evidence)
  212. {
  213. }
  214. #endif
  215. #endregion // Constructors
  216. #region Events
  217. private XmlAttributeEventHandler onUnknownAttribute;
  218. private XmlElementEventHandler onUnknownElement;
  219. private XmlNodeEventHandler onUnknownNode;
  220. private UnreferencedObjectEventHandler onUnreferencedObject;
  221. public event XmlAttributeEventHandler UnknownAttribute
  222. {
  223. add { onUnknownAttribute += value; } remove { onUnknownAttribute -= value; }
  224. }
  225. public event XmlElementEventHandler UnknownElement
  226. {
  227. add { onUnknownElement += value; } remove { onUnknownElement -= value; }
  228. }
  229. public event XmlNodeEventHandler UnknownNode
  230. {
  231. add { onUnknownNode += value; } remove { onUnknownNode -= value; }
  232. }
  233. public event UnreferencedObjectEventHandler UnreferencedObject
  234. {
  235. add { onUnreferencedObject += value; } remove { onUnreferencedObject -= value; }
  236. }
  237. internal virtual void OnUnknownAttribute (XmlAttributeEventArgs e)
  238. {
  239. if (onUnknownAttribute != null) onUnknownAttribute(this, e);
  240. }
  241. internal virtual void OnUnknownElement (XmlElementEventArgs e)
  242. {
  243. if (onUnknownElement != null) onUnknownElement(this, e);
  244. }
  245. internal virtual void OnUnknownNode (XmlNodeEventArgs e)
  246. {
  247. if (onUnknownNode != null) onUnknownNode(this, e);
  248. }
  249. internal virtual void OnUnreferencedObject (UnreferencedObjectEventArgs e)
  250. {
  251. if (onUnreferencedObject != null) onUnreferencedObject(this, e);
  252. }
  253. #endregion // Events
  254. #region Methods
  255. public virtual bool CanDeserialize (XmlReader xmlReader)
  256. {
  257. xmlReader.MoveToContent ();
  258. if (typeMapping is XmlMembersMapping)
  259. return true;
  260. else
  261. return ((XmlTypeMapping)typeMapping).ElementName == xmlReader.LocalName;
  262. }
  263. protected virtual XmlSerializationReader CreateReader ()
  264. {
  265. // Must be implemented in derived class
  266. throw new NotImplementedException ();
  267. }
  268. protected virtual XmlSerializationWriter CreateWriter ()
  269. {
  270. // Must be implemented in derived class
  271. throw new NotImplementedException ();
  272. }
  273. public object Deserialize (Stream stream)
  274. {
  275. XmlTextReader xmlReader = new XmlTextReader(stream);
  276. xmlReader.Normalization = true;
  277. return Deserialize(xmlReader);
  278. }
  279. public object Deserialize (TextReader textReader)
  280. {
  281. XmlTextReader xmlReader = new XmlTextReader(textReader);
  282. xmlReader.Normalization = true;
  283. return Deserialize(xmlReader);
  284. }
  285. public object Deserialize (XmlReader xmlReader)
  286. {
  287. XmlSerializationReader xsReader;
  288. if (customSerializer)
  289. xsReader = CreateReader ();
  290. else
  291. xsReader = CreateReader (typeMapping);
  292. xsReader.Initialize (xmlReader, this);
  293. return Deserialize (xsReader);
  294. }
  295. protected virtual object Deserialize (XmlSerializationReader reader)
  296. {
  297. if (customSerializer)
  298. // Must be implemented in derived class
  299. throw new NotImplementedException ();
  300. try {
  301. if (reader is XmlSerializationReaderInterpreter)
  302. return ((XmlSerializationReaderInterpreter) reader).ReadRoot ();
  303. else
  304. return serializerData.ReaderMethod.Invoke (reader, null);
  305. } catch (Exception ex) {
  306. if (ex is InvalidOperationException || ex is InvalidCastException)
  307. throw new InvalidOperationException ("There was an error generating" +
  308. " the XML document.", ex);
  309. throw;
  310. }
  311. }
  312. public static XmlSerializer [] FromMappings (XmlMapping [] mappings)
  313. {
  314. XmlSerializer[] sers = new XmlSerializer [mappings.Length];
  315. SerializerData[] datas = new SerializerData [mappings.Length];
  316. GenerationBatch batch = new GenerationBatch ();
  317. batch.Maps = mappings;
  318. batch.Datas = datas;
  319. for (int n=0; n<mappings.Length; n++)
  320. {
  321. if (mappings[n] != null)
  322. {
  323. SerializerData data = new SerializerData ();
  324. data.Batch = batch;
  325. sers[n] = new XmlSerializer (mappings[n], data);
  326. datas[n] = data;
  327. }
  328. }
  329. return sers;
  330. }
  331. public static XmlSerializer [] FromTypes (Type [] mappings)
  332. {
  333. XmlSerializer [] sers = new XmlSerializer [mappings.Length];
  334. for (int n=0; n<mappings.Length; n++)
  335. sers[n] = new XmlSerializer (mappings[n]);
  336. return sers;
  337. }
  338. protected virtual void Serialize (object o, XmlSerializationWriter writer)
  339. {
  340. if (customSerializer)
  341. // Must be implemented in derived class
  342. throw new NotImplementedException ();
  343. if (writer is XmlSerializationWriterInterpreter)
  344. ((XmlSerializationWriterInterpreter)writer).WriteRoot (o);
  345. else
  346. serializerData.WriterMethod.Invoke (writer, new object[] {o});
  347. }
  348. public void Serialize (Stream stream, object o)
  349. {
  350. XmlTextWriter xmlWriter = new XmlTextWriter (stream, System.Text.Encoding.Default);
  351. xmlWriter.Formatting = Formatting.Indented;
  352. Serialize (xmlWriter, o, null);
  353. }
  354. public void Serialize (TextWriter textWriter, object o)
  355. {
  356. XmlTextWriter xmlWriter = new XmlTextWriter (textWriter);
  357. xmlWriter.Formatting = Formatting.Indented;
  358. Serialize (xmlWriter, o, null);
  359. }
  360. public void Serialize (XmlWriter xmlWriter, object o)
  361. {
  362. Serialize (xmlWriter, o, null);
  363. }
  364. public void Serialize (Stream stream, object o, XmlSerializerNamespaces namespaces)
  365. {
  366. XmlTextWriter xmlWriter = new XmlTextWriter (stream, System.Text.Encoding.Default);
  367. xmlWriter.Formatting = Formatting.Indented;
  368. Serialize (xmlWriter, o, namespaces);
  369. }
  370. public void Serialize (TextWriter textWriter, object o, XmlSerializerNamespaces namespaces)
  371. {
  372. XmlTextWriter xmlWriter = new XmlTextWriter (textWriter);
  373. xmlWriter.Formatting = Formatting.Indented;
  374. Serialize (xmlWriter, o, namespaces);
  375. xmlWriter.Flush();
  376. }
  377. public void Serialize (XmlWriter writer, object o, XmlSerializerNamespaces namespaces)
  378. {
  379. XmlSerializationWriter xsWriter;
  380. try {
  381. if (customSerializer)
  382. xsWriter = CreateWriter ();
  383. else
  384. xsWriter = CreateWriter (typeMapping);
  385. if (namespaces == null || namespaces.Count == 0) {
  386. namespaces = new XmlSerializerNamespaces ();
  387. namespaces.Add ("xsd", XmlSchema.Namespace);
  388. namespaces.Add ("xsi", XmlSchema.InstanceNamespace);
  389. }
  390. xsWriter.Initialize (writer, namespaces);
  391. Serialize (o, xsWriter);
  392. writer.Flush ();
  393. } catch (Exception ex) {
  394. if (ex is TargetInvocationException)
  395. ex = ex.InnerException;
  396. if (ex is InvalidOperationException || ex is InvalidCastException)
  397. throw new InvalidOperationException ("There was an error generating" +
  398. " the XML document.", ex);
  399. throw;
  400. }
  401. }
  402. #if NET_2_0
  403. [MonoTODO]
  404. public object Deserialize (XmlReader xmlReader, string encodingStyle, XmlDeserializationEvents events)
  405. {
  406. throw new NotImplementedException ();
  407. }
  408. [MonoTODO]
  409. public object Deserialize (XmlReader xmlReader, string encodingStyle)
  410. {
  411. throw new NotImplementedException ();
  412. }
  413. [MonoTODO]
  414. public object Deserialize (XmlReader xmlReader, XmlDeserializationEvents events)
  415. {
  416. throw new NotImplementedException ();
  417. }
  418. [MonoTODO]
  419. public static XmlSerializer[] FromMappings (XmlMapping[] mappings, Evidence evidence)
  420. {
  421. throw new NotImplementedException ();
  422. }
  423. [MonoTODO]
  424. public static XmlSerializer[] FromMappings (XmlMapping[] mappings, Type type)
  425. {
  426. throw new NotImplementedException ();
  427. }
  428. public static Assembly GenerateSerializer (Type[] types, XmlMapping[] mappings)
  429. {
  430. return GenerateSerializer (types, mappings, null);
  431. }
  432. [MonoTODO]
  433. public static Assembly GenerateSerializer (Type[] types, XmlMapping[] mappings, CompilerParameters parameters)
  434. {
  435. GenerationBatch batch = new GenerationBatch ();
  436. batch.Maps = mappings;
  437. batch.Datas = new SerializerData [mappings.Length];
  438. for (int n=0; n<mappings.Length; n++) {
  439. SerializerData data = new SerializerData ();
  440. data.Batch = batch;
  441. batch.Datas [n] = data;
  442. }
  443. return GenerateSerializers (batch, parameters);
  444. }
  445. [MonoTODO]
  446. [Obsolete]
  447. public static Assembly GenerateSerializer (Type[] types,
  448. XmlMapping[] mappings,
  449. string codePath,
  450. bool debug,
  451. bool keepFiles)
  452. {
  453. throw new NotImplementedException ();
  454. }
  455. [MonoTODO]
  456. [Obsolete]
  457. public static Assembly GenerateSerializer (Type[] types,
  458. XmlMapping[] mappings,
  459. string codePath,
  460. bool debug,
  461. bool keepFiles,
  462. string compilerOptions)
  463. {
  464. throw new NotImplementedException ();
  465. }
  466. public static string GetXmlSerializerAssemblyName (Type type)
  467. {
  468. return type.Assembly.GetName().Name + ".XmlSerializers";
  469. }
  470. public static string GetXmlSerializerAssemblyName (Type type, string defaultNamespace)
  471. {
  472. return GetXmlSerializerAssemblyName (type) + "." + defaultNamespace.GetHashCode ();
  473. }
  474. [MonoTODO]
  475. public void Serialize (XmlWriter xmlWriter, object o, XmlSerializerNamespaces namespaces, string encodingStyle)
  476. {
  477. throw new NotImplementedException ();
  478. }
  479. #endif
  480. XmlSerializationWriter CreateWriter (XmlMapping typeMapping)
  481. {
  482. XmlSerializationWriter writer;
  483. lock (this) {
  484. if (serializerData != null) {
  485. lock (serializerData) {
  486. writer = serializerData.CreateWriter ();
  487. }
  488. if (writer != null) return writer;
  489. }
  490. }
  491. if (!typeMapping.Source.CanBeGenerated || generationThreshold == -1)
  492. return new XmlSerializationWriterInterpreter (typeMapping);
  493. CheckGeneratedTypes (typeMapping);
  494. lock (this) {
  495. lock (serializerData) {
  496. writer = serializerData.CreateWriter ();
  497. }
  498. if (writer != null) return writer;
  499. if (!generatorFallback)
  500. throw new InvalidOperationException ("Error while generating serializer");
  501. }
  502. return new XmlSerializationWriterInterpreter (typeMapping);
  503. }
  504. XmlSerializationReader CreateReader (XmlMapping typeMapping)
  505. {
  506. XmlSerializationReader reader;
  507. lock (this) {
  508. if (serializerData != null) {
  509. lock (serializerData) {
  510. reader = serializerData.CreateReader ();
  511. }
  512. if (reader != null) return reader;
  513. }
  514. }
  515. if (!typeMapping.Source.CanBeGenerated || generationThreshold == -1)
  516. return new XmlSerializationReaderInterpreter (typeMapping);
  517. CheckGeneratedTypes (typeMapping);
  518. lock (this) {
  519. lock (serializerData) {
  520. reader = serializerData.CreateReader ();
  521. }
  522. if (reader != null) return reader;
  523. if (!generatorFallback)
  524. throw new InvalidOperationException ("Error while generating serializer");
  525. }
  526. return new XmlSerializationReaderInterpreter (typeMapping);
  527. }
  528. #if TARGET_JVM
  529. void CheckGeneratedTypes (XmlMapping typeMapping)
  530. {
  531. throw new NotImplementedException();
  532. }
  533. void GenerateSerializersAsync (GenerationBatch batch)
  534. {
  535. throw new NotImplementedException();
  536. }
  537. void RunSerializerGeneration (object obj)
  538. {
  539. throw new NotImplementedException();
  540. }
  541. #else
  542. void CheckGeneratedTypes (XmlMapping typeMapping)
  543. {
  544. lock (this)
  545. {
  546. if (serializerData == null)
  547. {
  548. lock (serializerTypes)
  549. {
  550. serializerData = (SerializerData) serializerTypes [typeMapping.Source];
  551. if (serializerData == null) {
  552. serializerData = new SerializerData();
  553. serializerTypes [typeMapping.Source] = serializerData;
  554. }
  555. }
  556. }
  557. }
  558. bool generate = false;
  559. lock (serializerData)
  560. {
  561. generate = (++serializerData.UsageCount == generationThreshold);
  562. }
  563. if (generate)
  564. {
  565. if (serializerData.Batch != null)
  566. GenerateSerializersAsync (serializerData.Batch);
  567. else
  568. {
  569. GenerationBatch batch = new GenerationBatch ();
  570. batch.Maps = new XmlMapping[] {typeMapping};
  571. batch.Datas = new SerializerData[] {serializerData};
  572. GenerateSerializersAsync (batch);
  573. }
  574. }
  575. }
  576. void GenerateSerializersAsync (GenerationBatch batch)
  577. {
  578. if (batch.Maps.Length != batch.Datas.Length)
  579. throw new ArgumentException ("batch");
  580. lock (batch)
  581. {
  582. if (batch.Done) return;
  583. batch.Done = true;
  584. }
  585. if (backgroundGeneration)
  586. ThreadPool.QueueUserWorkItem (new WaitCallback (RunSerializerGeneration), batch);
  587. else
  588. RunSerializerGeneration (batch);
  589. }
  590. void RunSerializerGeneration (object obj)
  591. {
  592. try
  593. {
  594. GenerationBatch batch = (GenerationBatch) obj;
  595. batch = LoadFromSatelliteAssembly (batch);
  596. if (batch != null)
  597. GenerateSerializers (batch, null);
  598. }
  599. catch (Exception ex)
  600. {
  601. Console.WriteLine (ex);
  602. }
  603. }
  604. static Assembly GenerateSerializers (GenerationBatch batch, CompilerParameters cp)
  605. {
  606. DateTime tim = DateTime.Now;
  607. XmlMapping[] maps = batch.Maps;
  608. if (cp == null) {
  609. cp = new CompilerParameters();
  610. cp.IncludeDebugInformation = false;
  611. cp.GenerateInMemory = true;
  612. cp.TempFiles.KeepFiles = !deleteTempFiles;
  613. }
  614. string file = cp.TempFiles.AddExtension ("cs");
  615. StreamWriter sw = new StreamWriter (file);
  616. if (!deleteTempFiles)
  617. Console.WriteLine ("Generating " + file);
  618. SerializationCodeGenerator gen = new SerializationCodeGenerator (maps);
  619. try
  620. {
  621. gen.GenerateSerializers (sw);
  622. }
  623. catch (Exception ex)
  624. {
  625. Console.WriteLine ("Serializer could not be generated");
  626. Console.WriteLine (ex);
  627. cp.TempFiles.Delete ();
  628. return null;
  629. }
  630. sw.Close ();
  631. CSharpCodeProvider provider = new CSharpCodeProvider();
  632. ICodeCompiler comp = provider.CreateCompiler ();
  633. cp.GenerateExecutable = false;
  634. foreach (Type rtype in gen.ReferencedTypes)
  635. {
  636. if (!cp.ReferencedAssemblies.Contains (rtype.Assembly.Location))
  637. cp.ReferencedAssemblies.Add (rtype.Assembly.Location);
  638. }
  639. if (!cp.ReferencedAssemblies.Contains ("System.dll"))
  640. cp.ReferencedAssemblies.Add ("System.dll");
  641. if (!cp.ReferencedAssemblies.Contains ("System.Xml"))
  642. cp.ReferencedAssemblies.Add ("System.Xml");
  643. if (!cp.ReferencedAssemblies.Contains ("System.Data"))
  644. cp.ReferencedAssemblies.Add ("System.Data");
  645. CompilerResults res = comp.CompileAssemblyFromFile (cp, file);
  646. if (res.Errors.HasErrors || res.CompiledAssembly == null) {
  647. Console.WriteLine ("Error while compiling generated serializer");
  648. foreach (CompilerError error in res.Errors)
  649. Console.WriteLine (error);
  650. cp.TempFiles.Delete ();
  651. return null;
  652. }
  653. GenerationResult[] results = gen.GenerationResults;
  654. for (int n=0; n<results.Length; n++)
  655. {
  656. GenerationResult gres = results[n];
  657. SerializerData sd = batch.Datas [n];
  658. lock (sd)
  659. {
  660. sd.WriterType = res.CompiledAssembly.GetType (gres.Namespace + "." + gres.WriterClassName);
  661. sd.ReaderType = res.CompiledAssembly.GetType (gres.Namespace + "." + gres.ReaderClassName);
  662. sd.WriterMethod = sd.WriterType.GetMethod (gres.WriteMethodName);
  663. sd.ReaderMethod = sd.ReaderType.GetMethod (gres.ReadMethodName);
  664. sd.Batch = null;
  665. }
  666. }
  667. cp.TempFiles.Delete ();
  668. if (!deleteTempFiles)
  669. Console.WriteLine ("Generation finished - " + (DateTime.Now - tim).TotalMilliseconds + " ms");
  670. return res.CompiledAssembly;
  671. }
  672. #endif
  673. #if NET_2_0
  674. GenerationBatch LoadFromSatelliteAssembly (GenerationBatch batch)
  675. {
  676. return batch;
  677. }
  678. #else
  679. GenerationBatch LoadFromSatelliteAssembly (GenerationBatch batch)
  680. {
  681. return batch;
  682. }
  683. #endif
  684. #endregion // Methods
  685. }
  686. }