单独程序集中具有相同名称的嵌套类会导致序列化问题

发布于 2024-08-05 09:40:19 字数 836 浏览 1 评论 0原文

我正在使用一个设计不佳的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

原野 2024-08-12 09:40:19

如果您可以访问 .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

时光与爱终年不遇 2024-08-12 09:40:19

我找到了一个有效的答案。本文中的评论: 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文