C# mscorlib.XmlSerializers.DLL 的 FileNotFoundException,它不存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-01 08:43:59  来源:igfitidea点击:

FileNotFoundException for mscorlib.XmlSerializers.DLL, which doesn't exist

提问by Keith

I'm using an XmlSerializer to deserialize a particular type in mscorelib.dll

我正在使用 XmlSerializer 反序列化 mscorelib.dll 中的特定类型

XmlSerializer ser = new XmlSerializer( typeof( [.Net type in System] ) );
return ([.Net type in System]) ser.Deserialize( new StringReader( xmlValue ) );

This throws a caught FileNotFoundExceptionwhen the assembly is loaded:

这会FileNotFoundException在加载程序集时抛出一个错误:

"Could not load file or assembly 'mscorlib.XmlSerializers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The system cannot find the file specified."

“无法加载文件或程序集 'mscorlib.XmlSerializers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' 或其依赖项之一。系统找不到指定的文件。”

FusionLog:

融合日志:

=== Pre-bind state information ===
LOG: User = ###
LOG: DisplayName = mscorlib.XmlSerializers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=x86
 (Fully-specified)
LOG: Appbase = file:///C:/localdir
LOG: Initial PrivatePath = NULL
Calling assembly : System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\localdir\bin\Debug\appname.vshost.exe.Config
LOG: Using machine configuration file from c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\config\machine.config.
LOG: Post-policy reference: mscorlib.XmlSerializers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=x86
LOG: Attempting download of new URL file:///C:/localdir/bin/Debug/mscorlib.XmlSerializers.DLL.
LOG: Attempting download of new URL file:///C:/localdir/bin/Debug/mscorlib.XmlSerializers/mscorlib.XmlSerializers.DLL.
LOG: Attempting download of new URL file:///C:/localdir/bin/Debug/mscorlib.XmlSerializers.EXE.
LOG: Attempting download of new URL file:///C:/localdir/bin/Debug/mscorlib.XmlSerializers/mscorlib.XmlSerializers.EXE.

As far as I know there is no mscorlib.XmlSerializers.DLL, I think the DLL name has bee auto generated by .Net looking for the serializer.

据我所知,没有 mscorlib.XmlSerializers.DLL,我认为 DLL 名称已由 .Net 自动生成以寻找序列化程序。

You have the option of creating a myApplication.XmlSerializers.DLL when compiling to optimise serializations, so I assume this is part of the framework's checking for it.

在编译以优化序列化时,您可以选择创建 myApplication.XmlSerializers.DLL,因此我认为这是框架对其检查的一部分。

The problem is that this appears to be causing a delay in loading the application - it seems to hang for a few seconds at this point.

问题是这似乎导致了加载应用程序的延迟 - 此时它似乎挂了几秒钟。

Any ideas how to avoid this or speed it up?

任何想法如何避免这种情况或加快速度?

采纳答案by Will Dean

I'm guessing now. but:

我现在猜。但:

  1. The system might be generating a serializer for the whole of mscorlib, which could be very slow.
  2. You could probably avoid this by wrapping the system type in your own type and serialising that instead - then you'd get a serializer for your own assembly.
  3. You might be able to build the serializer for mscorlib with sgen.exe, which was the old way of building serializer dlls before it got integrated into VS.
  1. 系统可能正在为整个 mscorlib 生成序列化程序,这可能非常慢。
  2. 您可以通过将系统类型包装在您自己的类型中并对其进行序列化来避免这种情况 - 然后您将获得一个用于您自己的程序集的序列化程序。
  3. 您也许可以使用 sgen.exe 为 mscorlib 构建序列化程序,这是在将序列化程序 dll 集成到 VS 之前构建序列化程序 dll 的旧方法。

回答by Will Dean

The delay is because, having been unable to find the custom serializer dll, the system is building the equivalent code (which is very time-consuming) on the fly.

延迟是因为,由于无法找到自定义序列化程序 dll,系统正在即时构建等效代码(非常耗时)。

The way to avoid the delay is to have the system build the DLL, and make sure it's available to the .EXE - have you tried this?

避免延迟的方法是让系统构建 DLL,并确保它可用于 .EXE - 你试过这个吗?

回答by Will Dean

Okay, so I ran into this problem and have found a solution for it specific to my area.

好的,所以我遇到了这个问题,并找到了针对我所在地区的解决方案。

This occurred because I was trying to serialize a list into an XML document (file) without an XML root attribute. Once I added the following files, the error goes away.

发生这种情况是因为我试图将一个列表序列化为一个没有 XML 根属性的 XML 文档(文件)。一旦我添加了以下文件,错误就会消失。

XmlRootAttribute rootAttribute = new XmlRootAttribute();
rootAttribute.ElementName = "SomeRootName";
rootAttribute.IsNullable = true;

Dunno if it'll fix your problem, but it fixed mine.

不知道它是否会解决你的问题,但它解决了我的问题。