Forum guidelines
Problem with serialize in code complier
Posted By vahid bakhtiary on 28 Jan 2012
i use of this code for serialize in code compiler :
public void GetItemSource() { CSharpCodeProvider c = new CSharpCodeProvider(); ICodeCompiler icc = c.CreateCompiler(); CompilerParameters cp = new CompilerParameters(); cp.GenerateExecutable = false; cp.ReferencedAssemblies.Add(typeof(System.Data.EntityClient.EntityCommand).Assembly.Location); cp.ReferencedAssemblies.Add("system.dll"); cp.ReferencedAssemblies.Add("system.xml.dll"); cp.ReferencedAssemblies.Add(""); cp.ReferencedAssemblies.Add("System.Runtime.Serialization.Formatters.Soap.dll"); cp.ReferencedAssemblies.Add("system.drawing.dll"); cp.ReferencedAssemblies.Add("System.Core.dll"); cp.ReferencedAssemblies.Add("System.Xml.Linq.dll"); cp.ReferencedAssemblies.Add("System.Web.Services.dll"); cp.ReferencedAssemblies.Add("System.Runtime.Serialization.dll"); cp.CompilerOptions = "/t:library"; cp.GenerateInMemory = true; StringBuilder sb = new StringBuilder(""); sb.Append("using System;\n"); sb.Append("using System.Xml;\n"); sb.Append("using System.Data;\n"); sb.Append("using System.Data.SqlClient;\n"); sb.Append("using System.Linq;\n"); sb.Append("using System.Collections.Generic;\n"); sb.Append("using System.Runtime.Serialization;\n"); sb.Append("using System.Xml.Serialization;\n"); sb.Append("using System.IO.IsolatedStorage;\n"); sb.Append("using System.Runtime.Serialization.Formatters.Binary;"); sb.Append("using System.IO;\n"); sb.Append("using System.Text;\n"); sb.Append("[Serializable()] \n"); sb.Append(" public class QueryList \n"); sb.Append("{ \n"); sb.Append("public string Name {get; set;} \n"); sb.Append("} \n"); sb.Append("public class GetData{ \n"); sb.Append("public string ItemSource(){\n"); sb.Append(" List<QueryList> list = new List<QueryList>();\n"); sb.Append(@" list.Add(new QueryList() { ID =""Jenifer"" });"); sb.Append(@" list.Add(new QueryList() { ID =""Adam"" });"); sb.Append(" string str = ToXml(list);\n"); sb.Append(" return str;\n"); sb.Append("}"); sb.Append("public string ToXml(List<QueryList> pparam)\n"); sb.Append("{\n"); sb.Append(" XmlSerializer serializer = new XmlSerializer(typeof(List<QueryList>));\n"); sb.Append("StringBuilder stringBuilder = new StringBuilder();\n"); sb.Append("XmlWriterSettings settings = new XmlWriterSettings()\n"); sb.Append(" {\n"); sb.Append("Indent = true,\n"); sb.Append("OmitXmlDeclaration = true\n"); sb.Append(" };\n"); sb.Append(" using (XmlWriter xmlWriter = XmlWriter.Create(stringBuilder))\n"); sb.Append(" {\n"); sb.Append(" serializer.Serialize(xmlWriter, pparam);\n"); sb.Append(" }\n"); sb.Append(" return stringBuilder.ToString();\n"); sb.Append(" }\n"); sb.Append("}\n"); string test = sb.ToString(); CompilerResults cr = icc.CompileAssemblyFromSource(cp, sb.ToString()); System.Reflection.Assembly a = cr.CompiledAssembly; object o = a.CreateInstance("GetData"); Type t = o.GetType(); MethodInfo mi = t.GetMethod("ItemSource"); string str = (string)mi.Invoke(o, null); }

i end line i meet error
but when i that code run without code compiler every thing is ok
but in code compiler i don't serialize it
please help me

Re: Problem with serialize in code complier
Posted By Jignesh Trivedi on 30 Jan 2012  

Please replace two in above code (ID Property does not exist).
sb.Append(@" list.Add(new QueryList() { Name =""Jenifer"" });");
sb.Append(@" list.Add(new QueryList() { Name =""Adam"" });"); sb.Append(" string str = ToXml(list);\n");

and following line not work in ur code.

sb.Append(" serializer.Serialize(xmlWriter, pparam);\n");

find alternate for same.

hope this help.

Training for .Net Developers


JetBrains ReSharper Ultimate
.NET code analysis, refactorings, navigation, unit testing, code coverage and profiling in Visual Studio, a bonus for C++ devs, bundled at 50% off.