GenerateSerializers() public method

public GenerateSerializers ( TextWriter writer ) : void
writer System.IO.TextWriter
return void
Exemplo n.º 1
0
		static Assembly GenerateSerializers (GenerationBatch batch, CompilerParameters cp)
		{
			DateTime tim = DateTime.Now;
			
			XmlMapping[] maps = batch.Maps;
			
			if (cp == null) {
				cp = new CompilerParameters();
				cp.IncludeDebugInformation = false;
				cp.GenerateInMemory = true;
				cp.TempFiles.KeepFiles = !deleteTempFiles;
			}
			
			string file = cp.TempFiles.AddExtension ("cs");
			StreamWriter sw = new StreamWriter (file);
			
			if (!deleteTempFiles)
				Console.WriteLine ("Generating " + file);
			
			SerializationCodeGenerator gen = new SerializationCodeGenerator (maps);
			
			try
			{
				gen.GenerateSerializers (sw);
			}
			catch (Exception ex)
			{
				Console.WriteLine ("Serializer could not be generated");
				Console.WriteLine (ex);
				cp.TempFiles.Delete ();
				return null;
			}
			sw.Close ();
			
			CSharpCodeProvider provider = new CSharpCodeProvider();
			ICodeCompiler comp = provider.CreateCompiler ();
			
			cp.GenerateExecutable = false;
			
			foreach (Type rtype in gen.ReferencedTypes)
			{
				string path = new Uri (rtype.Assembly.CodeBase).LocalPath;
				if (!cp.ReferencedAssemblies.Contains (path))
					cp.ReferencedAssemblies.Add (path);
			}
				
			if (!cp.ReferencedAssemblies.Contains ("System.dll"))
				cp.ReferencedAssemblies.Add ("System.dll");
			if (!cp.ReferencedAssemblies.Contains ("System.Xml"))
				cp.ReferencedAssemblies.Add ("System.Xml");
			if (!cp.ReferencedAssemblies.Contains ("System.Data"))
				cp.ReferencedAssemblies.Add ("System.Data");
			if (!cp.ReferencedAssemblies.Contains ("System.Web.Services"))
				cp.ReferencedAssemblies.Add ("System.Web.Services");
			
			CompilerResults res = comp.CompileAssemblyFromFile (cp, file);
			if (res.Errors.HasErrors || res.CompiledAssembly == null) {
				Console.WriteLine ("Error while compiling generated serializer");
				foreach (CompilerError error in res.Errors)
					Console.WriteLine (error);
					
				cp.TempFiles.Delete ();
				return null;
			}
			
			GenerationResult[] results = gen.GenerationResults;
			for (int n=0; n<results.Length; n++)
			{
				GenerationResult gres = results[n];
				SerializerData sd = batch.Datas [n];
				lock (sd)
				{
					sd.WriterType = res.CompiledAssembly.GetType (gres.Namespace + "." + gres.WriterClassName);
					sd.ReaderType = res.CompiledAssembly.GetType (gres.Namespace + "." + gres.ReaderClassName);
					sd.WriterMethod = sd.WriterType.GetMethod (gres.WriteMethodName);
					sd.ReaderMethod = sd.ReaderType.GetMethod (gres.ReadMethodName);
					sd.Batch = null;
				}
			}
			
			cp.TempFiles.Delete ();

			if (!deleteTempFiles)
				Console.WriteLine ("Generation finished - " + (DateTime.Now - tim).TotalMilliseconds + " ms");
				
			return res.CompiledAssembly;
		}
Exemplo n.º 2
0
		public static void Generate (string configFileName, string outputPath)
		{
			SerializationCodeGeneratorConfiguration cnf = null;
			StreamReader sr = new StreamReader (configFileName);
			try
			{
				XmlReflectionImporter ri = new XmlReflectionImporter ();
				ri.AllowPrivateTypes = true;
				XmlSerializer ser = new XmlSerializer (ri.ImportTypeMapping (typeof (SerializationCodeGeneratorConfiguration)));
				cnf = (SerializationCodeGeneratorConfiguration) ser.Deserialize (sr);
			}
			finally
			{
				sr.Close ();
			}
			
			if (outputPath == null) outputPath = "";
			
			CodeIdentifiers ids = new CodeIdentifiers ();
			if (cnf.Serializers != null)
			{
				foreach (SerializerInfo info in cnf.Serializers)
				{					
					Type type;
					if (info.Assembly != null)
					{
						Assembly asm;
						try {
							asm = Assembly.Load (info.Assembly);
						} catch {
							asm = Assembly.LoadFrom (info.Assembly);
						}
						type = asm.GetType (info.ClassName, true);
					}
					else
						type = Type.GetType (info.ClassName);
					
					if (type == null) throw new InvalidOperationException ("Type " + info.ClassName + " not found");
					
					string file = info.OutFileName;
					if (file == null || file.Length == 0) {
						int i = info.ClassName.LastIndexOf (".");
						if (i != -1) file = info.ClassName.Substring (i+1);
						else file = info.ClassName;
						file = ids.AddUnique (file, type) + "Serializer.cs";
					}
					StreamWriter writer = new StreamWriter (Path.Combine (outputPath, file));
					
					try
					{
						XmlTypeMapping map;
						
						if (info.SerializationFormat == SerializationFormat.Literal) {
							XmlReflectionImporter ri = new XmlReflectionImporter ();
							map = ri.ImportTypeMapping (type);
						}
						else {
							SoapReflectionImporter ri = new SoapReflectionImporter ();
							map = ri.ImportTypeMapping (type);
						}
						
						SerializationCodeGenerator gen = new SerializationCodeGenerator (map, info);
						gen.GenerateSerializers (writer);
					}
					finally
					{
						writer.Close ();
					}
				}
			}
		}
		void RunSerializerGenerationAux (object obj)
		{
			DateTime tim = DateTime.Now;
			
			GenerationBatch batch = (GenerationBatch) obj;
			XmlMapping[] maps = batch.Maps;
			
			string file = Path.GetTempFileName ();
			StreamWriter sw = new StreamWriter (file);
			
			if (!deleteTempFiles)
				Console.WriteLine ("Generating " + file);
			
			SerializationCodeGenerator gen = new SerializationCodeGenerator (maps);
			
			try
			{
				gen.GenerateSerializers (sw);
			}
			catch (Exception ex)
			{
				Console.WriteLine ("Serializer could not be generated");
				Console.WriteLine (ex);
				
				if (deleteTempFiles)
					File.Delete (file);
				return;
			}
			sw.Close ();
			
			CSharpCodeProvider provider = new CSharpCodeProvider();
			ICodeCompiler comp = provider.CreateCompiler ();
			
			CompilerParameters cp = new CompilerParameters();
		    cp.GenerateExecutable = false;
			cp.IncludeDebugInformation = false;
		    cp.GenerateInMemory = true;
		    cp.ReferencedAssemblies.Add ("System.dll");
			cp.ReferencedAssemblies.Add ("System.Xml");
			cp.ReferencedAssemblies.Add ("System.Data");
			
			foreach (Type rtype in gen.ReferencedTypes)
			{
				if (!cp.ReferencedAssemblies.Contains (rtype.Assembly.Location))
					cp.ReferencedAssemblies.Add (rtype.Assembly.Location);
			}

			CompilerResults res = comp.CompileAssemblyFromFile (cp, file);
			if (res.Errors.Count > 0)
			{
				Console.WriteLine ("Error while compiling generated serializer");
				foreach (CompilerError error in res.Errors)
					Console.WriteLine (error);
					
				if (deleteTempFiles)
					File.Delete (file);
				return;
			}
			
			GenerationResult[] results = gen.GenerationResults;
			for (int n=0; n<results.Length; n++)
			{
				GenerationResult gres = results[n];
				SerializerData sd = batch.Datas [n];
				lock (sd)
				{
					sd.WriterType = res.CompiledAssembly.GetType (gres.Namespace + "." + gres.WriterClassName);
					sd.ReaderType = res.CompiledAssembly.GetType (gres.Namespace + "." + gres.ReaderClassName);
					sd.WriterMethod = sd.WriterType.GetMethod (gres.WriteMethodName);
					sd.ReaderMethod = sd.ReaderType.GetMethod (gres.ReadMethodName);
					sd.Batch = null;
				}
			}
			
			if (deleteTempFiles)
				File.Delete (file);

			if (!deleteTempFiles)
				Console.WriteLine ("Generation finished - " + (DateTime.Now - tim).TotalMilliseconds + " ms");
		}