现在的位置: 首页 > 综合 > 正文

XML 对象反序列化也动态编译?

2013年10月15日 ⁄ 综合 ⁄ 共 3527字 ⁄ 字号 评论关闭

今天碰到了个非常奇怪的问题,而且无法重现。

在一个类构造的时候从 xml 文件反序列化一个对象。一般情况下都是好,极少数情况下会出现一下问题。

System.Runtime.InteropServices.ExternalException: Timed out waiting for a program to execute. The command being executed was "c:/windows/microsoft.net/framework/v1.1.4322/csc.exe" /noconfig @"C:/DOCUME~1/montaq~1/LOCALS~1/Temp/prfmes01.cmdline".
   at System.CodeDom.Compiler.Executor.ExecWaitWithCaptureUnimpersonated(IntPtr userToken, String cmd, String currentDir, TempFileCollection tempFiles, String& outputName, String& errorName, String trueCmdLine)
   at System.CodeDom.Compiler.Executor.ExecWaitWithCapture(IntPtr userToken, String cmd, String currentDir, TempFileCollection tempFiles, String& outputName, String& errorName, String trueCmdLine)
   at System.CodeDom.Compiler.CodeCompiler.Compile(CompilerParameters options, String compilerDirectory, String compilerExe, String arguments, String& outputFile, Int32& nativeReturnValue, String trueArgs)
   at System.CodeDom.Compiler.CodeCompiler.FromFileBatch(CompilerParameters options, String[] fileNames)
   at System.CodeDom.Compiler.CodeCompiler.FromSourceBatch(CompilerParameters options, String[] sources)
   at System.CodeDom.Compiler.CodeCompiler.FromSource(CompilerParameters options, String source)
   at System.CodeDom.Compiler.CodeCompiler.System.CodeDom.Compiler.ICodeCompiler.CompileAssemblyFromSource(CompilerParameters options, String source)
   at System.Xml.Serialization.Compiler.Compile()
   at System.Xml.Serialization.TempAssembly..ctor(XmlMapping[] xmlMappings)
   at System.Xml.Serialization.XmlSerializer..ctor(Type type, String defaultNamespace)
   at System.Xml.Serialization.XmlSerializer..ctor(Type type)
   at XmlState..ctor(String path)
   at AutoUpdateingAddin.Updater..ctor(String hostAppExeFullPath)
   at AutoUpdateingAddin.Form1.CheckUpdate()
   at AutoUpdateingAddin.Form1.Form1_Load(Object sender, EventArgs e)
   at System.Windows.Forms.Form.OnLoad(EventArgs e)
   at System.Windows.Forms.Form.OnCreateControl()
   at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
   at System.Windows.Forms.Control.CreateControl()
   at System.Windows.Forms.Control.WmShowWindow(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.ContainerControl.WndProc(Message& m)
   at System.Windows.Forms.Form.WmShowWindow(Message& m)
   at System.Windows.Forms.Form.WndProc(Message& m)
   at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

xml 文件不便,程序也不变,有时候就会有这样的错误。

从这个错误看出来,对象反序列化的时候,会调用动态编译,首先动态的生成一些代码,然后再去编译执行。hoho,中间结果都放在temp文件中。执行结束的时候,delete他。

用reflector 验证了一下,果然如此。(y)

为了测试,我写一个简单的反序列化程序

新建一个类

using System;
using System.Xml;
using System.Xml.Serialization;

namespace XmlDemo
{

    [XmlRoot(
"Person",Namespace="")]
    
public class Person
    
{    
        [XmlElement(
"PersonName")]
        
public string Name;

        
public int Age;
    }

}

然后我生成一个该类的实例并把它写道xml 文件。文件内容如下

 <?xml version="1.0" ?> 
<Person xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  
<PersonName>Montaque</PersonName> 
  
<Age>30</Age> 
  
</Person>

接下来就是反序列化这个类,记住要引用 System.runtime.Serialization.dll

代码如下

System.Xml.Serialization.XmlSerializer xr=new System.Xml.Serialization.XmlSerializer(typeof(Person)) ;
System.IO.FileStream fs
=new System.IO.FileStream("c://test.xml",System.IO.FileMode.Open ,System.IO.FileAccess.Read);
Person p 
=(Person)xr.Deserialize(fs);
Console.WriteLine(p.Name);

运行结果没有问题。

运行过程中,我用文件监视器坚实了一下csc.exe ,果然他先生成一下中间的cs,然后编译。。。

如下图

抱歉!评论已关闭.