分享
 
 
 

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

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

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

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

如下图

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有