当从父应用程序动态加载的程序集调用时,XMLSerializer 无法保存/加载?
我正在为应用程序编写一个插件 dll。应用程序通过以下方式加载插件程序集:
assembly = Assembly.Load(System.IO.File.ReadAllBytes(filename));
当我尝试序列化/反序列化公共类时,问题就会显现出来。我将其范围缩小到序列化:
public BindingList<MyClass> MyClasses
{ get; set; }
如果我将其注释掉,就没有问题。如果我尝试序列化:
public static void SaveSettingsFile()
{
try
{
XmlSerializer ser = new XmlSerializer(typeof(GameTimeSettings));
TextWriter writer = new StreamWriter(SettingPath);
ser.Serialize(writer, Settings.Instance);
writer.Close();
}
catch (Exception e)
{
Logger.ReportException("SaveSettingsFile", e);
Logger.ReportException("SaveSettingsFile->InnerException", e.InnerException);
}
}
ser.Serialize(writer, Settings.Instance) 抛出异常:
System.InvalidOperationException Msg=There is an error in XML document (0,, 0). ->
InnerException: Object reference not set to an instance of an object.
我的类有一个默认的空构造函数。我尝试过使用 sgen。在我编写的一个简单的测试台应用程序中,序列化工作正常......只有当程序集动态加载时才会出错。
此外,从这两个线程中,
http://forums.gbpvr .com/showthread.php?30384-XMLSerializer-Problems-with-Plugins , http://forums.gbpvr.com/showthread.php?32197-System.XML-Deserialization
我知道我可以将类型从 BindingList 更改为 ArrayList 并使其工作;但是,我想继续数据绑定工作,因为有很多设置需要管理。
任何意见将不胜感激。
I am writing a plugin dll for an application. The application loads plugin assemblies via:
assembly = Assembly.Load(System.IO.File.ReadAllBytes(filename));
The problem manifests itself when I attempt to serialize/deserialize a plublic class. I narrowed it down to the serialization of:
public BindingList<MyClass> MyClasses
{ get; set; }
If I comment this out, no problems. If I attempt to serialize with:
public static void SaveSettingsFile()
{
try
{
XmlSerializer ser = new XmlSerializer(typeof(GameTimeSettings));
TextWriter writer = new StreamWriter(SettingPath);
ser.Serialize(writer, Settings.Instance);
writer.Close();
}
catch (Exception e)
{
Logger.ReportException("SaveSettingsFile", e);
Logger.ReportException("SaveSettingsFile->InnerException", e.InnerException);
}
}
An exception is thrown on ser.Serialize(writer, Settings.Instance) :
System.InvalidOperationException Msg=There is an error in XML document (0,, 0). ->
InnerException: Object reference not set to an instance of an object.
My class has a default, empty constructor. I have tried using sgen. In a simple testbed application I wrote, serialization works fine... it's only when the Assembly is dynamically loaded does it fault.
Furthermore, from these two threads,
http://forums.gbpvr.com/showthread.php?30384-XMLSerializer-Problems-with-Plugins , http://forums.gbpvr.com/showthread.php?32197-System.XML-Deserialization
I know I can change the type from BindingList to ArrayList and have it work; however, I would like to keep databinding working as there are quite a bit of settings to manage.
Any input would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
到目前为止,John Saunders 的评论似乎是正确的,您可能没有初始化
MyClasses
。既然你说它与 SGen 配合得很好,你可能还想检查它是否与编译标志有关,比如尝试将其设置为发布,看看是否会改变事情。
John Saunders' comment appears correct for this so far, you likely are not initializing
MyClasses
.Since you say it works fine with SGen, you might also want to check if it has something to do with the compile flags, like maybe try setting it to release and see if that changes things.
很抱歉把这个从坟墓里拉出来,但我今天再次遇到了类似的问题,这是第一个谷歌结果,所以我想我会分享。
这似乎是我不久前尝试编写 MSBuild 自定义任务(基本上是一个插件)时遇到的问题的一般情况。我在 MSDN 上发布了有关它的信息论坛。
这是评论中最相关的部分,只需将 MSBuild 替换为任何具有插件架构的应用程序,并将 CruiseControl.NET 库替换为任何使用 XmlSerializer 的库:
MSDN 论坛帖子中提供的解决方案也适用于此处,基本上他们添加了一个额外的事件处理程序来尝试从已知位置进行探测,我再次引用了相关部分:
就我个人而言,这让我感到害怕,但这是我发现的唯一有效的解决方案,更多的谷歌搜索并没有向我显示 Microsoft Connect 问题,但它很可能不会被修复或者是设计使然。
Sorry to pull this up from the grave, but I ran into a similar issue again today and this was the first google result, so I thought I'd share.
This appears to be a generic case of an issue I had ran into awhile back when attempting to write an MSBuild Custom task (basically a plugin). I posted about it on the MSDN Forums.
Here is the most relevant part of the comment, simply substitute MSBuild for any application that has a plugin architecture, and the CruiseControl.NET library for any library that uses the XmlSerializer:
The solution offered in the MSDN Forum post is also applicable here, basically they added an additional event handler to attempt to probe from a known location, again I have quoted the relevant portions:
Personally this scares me, but it is the only solution I have found that works, a bit more googling hasn't shown me a Microsoft Connect issue for this, but it is most likely not going to be fixed or is by design.