Vahid Bakhtiary

Vahid Bakhtiary

  • NA
  • 143
  • 92.9k

Problem with serialize in code complier

Jan 28 2012 10:17 AM
hi
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("system.data.dll"); 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
thanks



Answers (1)