.NET开发

本类阅读TOP10

·NHibernate快速指南(翻译)
·vs.net 2005中文版下载地址收藏
·【小技巧】一个判断session是否过期的小技巧
·VB/ASP 调用 SQL Server 的存储过程
·?dos下编译.net程序找不到csc.exe文件
·通过Web Services上传和下载文件
·学习笔记(补)《.NET框架程序设计(修订版)》--目录
·VB.NET实现DirectDraw9 (2) 动画
·VB.NET实现DirectDraw9 (1) 托管的DDraw
·建站框架规范书之——文件命名

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
XML 对象反序列化也动态编译?

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

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

在一个类构造的时候从 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,然后编译。。。

如下图




相关文章

相关软件