单独程序集中具有相同名称的嵌套类会导致序列化问题
我正在使用一个设计不佳的 API。我有一个需要序列化的类,并且我可以控制该类的构成,但不能控制构成该类正在序列化的属性的类型。一个例子如下:
<Project>
<SomeProperty1 />
<Install>
<DefaultStep></DefaultStep>
</Install>
<Uninstall>
<DefaultStep></DefaultStep>
</Uninstall>
</Project>
问题是,我无法控制“Install”和“Uninstall”类型,并且它们的嵌套类型具有相同的名称。 “安装”驻留在 MyCompany.Install.dll 中,“卸载”驻留在 MyCompany.Uninstall.dll 中。但更关键的是,MyCompany.Uninstall.dll 引用了 MyCompany.Install.dll,这是完全没有意义的。 我知道这是糟糕的设计(我正在处理的整个框架很糟糕),但我没有选择使用它。
我得到的错误是:
“类型 'MyCompany.Install.Uninstall.DefaultStep' 和“MyCompany.Install.DefaultStep” 两者都使用 XML 类型名称, “DefaultStep”,来自命名空间“”。使用 XML 属性指定唯一的 XML 类型的名称和/或命名空间。”
这将是一个好主意,除了我对包含“安装”和“卸载”类的程序集的控制为零。
有什么想法吗?
I’m working with a poorly-engineered API. I have a class that I need to serialize, and I have control over the makeup of the class, but not the types that make up the properties the class is serialzing. An example is below:
<Project>
<SomeProperty1 />
<Install>
<DefaultStep></DefaultStep>
</Install>
<Uninstall>
<DefaultStep></DefaultStep>
</Uninstall>
</Project>
The problem is, I have no control over the “Install” and “Uninstall” types, and their nested types have the same name. "Install" resides in MyCompany.Install.dll, and "Uninstall" resides in MyCompany.Uninstall.dll. But the kicker is, MyCompany.Uninstall.dll refererences MyCompany.Install.dll, which is completely pointless.
I know this is bad design (the whole Framework I’m dealing with is terrible) but I don’t have a choice in working with it.
The error I get is:
"Types
'MyCompany.Install.Uninstall.DefaultStep'
and 'MyCompany.Install.DefaultStep'
both use the XML type name,
'DefaultStep', from namespace ''. Use
XML attributes to specify a unique XML
name and/or namespace for the type."
That would be a nice idea, except I have zero control over the assemblies that contain the "Install" and "Uninstall" classes.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您可以访问 .NET 3.5,我将使用 DataContract 序列化程序,并实现 IDataContractSurrogate。代理序列化允许您在序列化时用替代类型替换导致序列化混乱的挑剔类型。您对代孕妈妈有完全的控制权。这应该可以帮助您解决问题。
http://msdn.microsoft.com/en-我们/library/system.runtime.serialization.idatacontractsurrogate.aspx
If you have access to .NET 3.5, I would use the DataContract serializer, and implement an IDataContractSurrogate. Surrogate serialization allows you to replace a fussy type that is screwing up serialization with an alternative type at serialization time. You have full control over the surrogate. That should help you solve the problem.
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.idatacontractsurrogate.aspx
我找到了一个有效的答案。本文中的评论: http://www.codeproject.com/KB/XML/xmlserializerforunknown .aspx
让我看到了这篇文章:
http://mfharoon.blogspot.com/2006/ 12/using-ixmlserialized-to-overcome-not.html
唯一需要更改的是第 108 行,该行需要读取:
writer.WriteAttributeString("type", _parameters.GetType().AssemblyQualifiedName.ToString ());
如果类型位于单独的程序集中,这将使序列化工作。
哈!
I found an answer that works. Comments in this article: http://www.codeproject.com/KB/XML/xmlserializerforunknown.aspx
Led me to this article:
http://mfharoon.blogspot.com/2006/12/using-ixmlserializable-to-overcome-not.html
The only thing you need to change is line 108, which needs to read:
writer.WriteAttributeString("type", _parameters.GetType().AssemblyQualifiedName.ToString());
That will let the serialization work if the type is in a separate assembly.
HTH!