DataContractSerializer,为什么是“this”?不向上吗?
我正在使用 F# 编码的服务器上工作。该服务器连接到使用 C# 编码的 WCF REST 服务。
我的 F# 项目正在引用 C# 程序集。
在我的 C# 中,我有命名空间 GlobalNotificationService.DTO
:
public class Producer
{
[DataMember(Name = "Name", IsRequired = true, Order = 1)]
public string Name {get; set;}
...
public string Marshal()
{
StringBuilder sb = new StringBuilder();
using (XmlTextWriter textWriter = new XmlTextWriter(new StringWriter(sb)))
{
DataContractSerializer serializer = new DataContractSerializer(typeof(DTO.Producer));
serializer.WriteObject(textWriter, this); //THIS IS THE LINE THROWING THE EXECPTION
textWriter.Flush();
textWriter.Close();
return sb.ToString();
}
}
}
在我的 F# 中,我有命名空间 ServiceCore
:
type Producer(file_name) as this =
inherit DTO.Producer()
...
member public this.SerializedXML
with get() = this.Marshal() //Call to C# assembly
因此代码确实可以编译,但我在运行时遇到此异常(查找上面的注释)。
输入'ServiceCore.Producer'以及数据合约名称 “生产者:http://schemas.datacontract.org/2004/07/ServiceCore” 不是 预期的。考虑使用 DataContractResolver 或添加任何类型 静态已知已知类型列表 - 例如,通过使用 KnownTypeAttribute 属性或将它们添加到列表中 传递给 DataContractSerializer 的已知类型。
问题是:
为什么“this”类型是 ServiceCode.Producer (F#) 即使它应该被升级为 DTO.Producer < em>(C#) 因为该方法位于 DTO.Producer 类中,并且我明确要求 DTO.Producer 序列化?
补充问题:
为什么我们甚至关心调用来自后代类的事实?我们的“this”引用仍应向上转换并且类型为DTO.Producer。
这对我来说没有多大意义,我错过了什么吗?
I'm working on a server coded with F#. This server connects to a WCF REST service coded with C#.
My F# project is referencing the C# assembly.
In my C# I have namespace GlobalNotificationService.DTO
:
public class Producer
{
[DataMember(Name = "Name", IsRequired = true, Order = 1)]
public string Name {get; set;}
...
public string Marshal()
{
StringBuilder sb = new StringBuilder();
using (XmlTextWriter textWriter = new XmlTextWriter(new StringWriter(sb)))
{
DataContractSerializer serializer = new DataContractSerializer(typeof(DTO.Producer));
serializer.WriteObject(textWriter, this); //THIS IS THE LINE THROWING THE EXECPTION
textWriter.Flush();
textWriter.Close();
return sb.ToString();
}
}
}
In my F# I have namespace ServiceCore
:
type Producer(file_name) as this =
inherit DTO.Producer()
...
member public this.SerializedXML
with get() = this.Marshal() //Call to C# assembly
So the code does compile but I get this exception at run time (look for the comment above).
Type 'ServiceCore.Producer' with data contract name
'Producer:http://schemas.datacontract.org/2004/07/ServiceCore' is not
expected. Consider using a DataContractResolver or add any types not
known statically to the list of known types - for example, by using
the KnownTypeAttribute attribute or by adding them to the list of
known types passed to DataContractSerializer.
The question is:
Why is "this" of type ServiceCode.Producer (F#) even though it should have been upcasted to DTO.Producer (C#) since the method is in the DTO.Producer class and that I'm explicitly asking for a DTO.Producer serialization?
Complementary question:
Why do we even care about the fact that the call is coming from a descendant class? Our "this" reference should still be up-casted and be of type DTO.Producer.
It doesn't make a lot of sence to me, am I missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您在 Marshal 中显式实例化 DCS,因此您可以将其定义更改为 to
,然后 F# 代码可以使用其自己的子类型调用 Marshal。
Since you're explicitly instantiating the DCS in Marshal, you could change its definition to to be
and then the F# code could call Marshal with its own subtype.
序列化中没有自动向上转型。您必须明确告诉序列化器哪些类型与其他类型等效。
对于基本类型 (DTO.Producer),您必须添加一个。
您也可以尝试在 Marshal 调用中将对象显式转换为 DTO.Producer,但这可能不起作用。
There is no automatic upcast in serialization. You have to tell the serializer explicitly what types are equivalent to other types.
For the base type (DTO.Producer) you have to add a
You might also be able to try and explicitly cast the object to DTO.Producer in the Marshal call, but that might not work.