WCF REST:使用“WITHOUT”类的 ReadAsDataContract装饰属性 DataContract 和 DataMember

发布于 2024-09-27 20:34:42 字数 701 浏览 5 评论 0原文

我刚刚做了一些测试,我已经设法让 ReadAsDataContract 在 Response.Content 方法上工作......

真正令人困惑的是我认为它还不能工作!因为我的类没有用 DataContact 或 DataMember 属性装饰。

我很困惑,阅读网络上的各种教程,似乎用 DataContract 和 DataMember 属性装饰你的类(用于 ReadAsDataContract)很重要。

但我的不是,我在调用 ReadAsDataContract 的行上放置了一个断点,并且确定足够了,我已经返回了包含正确详细信息的列表。这是

List<Models.Question> questions = response.Content.ReadAsDataContract<List<Models.Question>>();

Models.Question 行没有用属性修饰的情况。我与我的服务器和客户端共享我的模型组件。

有人可以告诉我为什么它会起作用,据我所知,您需要在类和成员上添加属性(我没有)

我正在使用 Visual Studio 2010 和 .NET 4 - ermmm 我似乎记得这些属性是不再需要 - 这是真的吗?

真的期待任何帮助。

我讨厌当事情有效而他们不应该:-)但当然我也讨厌当这不工作而他们应该:-)

谢谢!

I have just done some tests and i have manged to get ReadAsDataContract working on the Response.Content method...

The thing that really is confusing is that i thought it shouldn't work yet! As my classes are NOT decorated with the DataContact or DataMember Attributes.

I am confused, reading the various tutorials around the web it seems that it is IMPORTANT to decorate your class (used for ReadAsDataContract) with DataContract and DataMember attributes..

BUT mine is not and i put a breakpoint on the line that calls ReadAsDataContract and sure enough i have a LIST returned with the correct details. Here is the line

List<Models.Question> questions = response.Content.ReadAsDataContract<List<Models.Question>>();

Models.Question is not decorated with the attributes. I share my model assembly with both my server and my client.

Can somebody tell me why it works when as far as i know you need to add the attributes on the class and members (which i have not)

I am using Visual Studio 2010 and .NET 4 - ermmm i seem to remember that these attributes are no longer required - is this true??

Really look forward to any help.

I hate it when things WORK and they SHOULD NOT :-) but of course I also hate when this DON'T WORK when they SHOULD :-)

THanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

木落 2024-10-04 20:34:42

有人可以告诉我为什么它会起作用吗?
据我所知你需要添加
类和成员的属性
(我没有)

对于全世界的程序员来说,将 [DataContract] 放在数据类上并将 [DataMember] 放在要序列化的每个成员上的“负担”似乎太大了 - 因此 Microsoft 更改了 .NET 3.5 中 DataContractSerializer 的行为SP1。

从那时起,DCS 的行为将像旧的 XmlSerializer 一样,具有未修饰的类 - 它将愉快地序列化所有公共成员和属性。

当然,这样做的问题是:您失去了属性为您提供的所有附加控制,例如为数据协定定义 XML 命名空间、定义数据成员的顺序等 - 但它无需添加任何属性即可工作。

所以这是一种众所周知的、自愿引入的行为。我仍然认为您应该明确您的意图,并使用 [DataContract] 标记您的数据类,并使用 [DataMember] 标记您的成员(这也使您能够忽略一个或多个公共成员,并且不将它们包含在序列化中)。

Can somebody tell me why it works when
as far as i know you need to add the
attributes on the class and members
(which i have not)

It seems the "burden" of putting [DataContract] on data classes and [DataMember] on each member to be serialized was too much for the programmers worldwide - so Microsoft changed the behavior of the DataContractSerializer in .NET 3.5 SP1.

Since that time, the DCS will behave like the old XmlSerializer with a class that isn't decorated - it will happily serialize all public members and properties.

Problem with that is, of course: you loose all the additional control that the attributes give you, like defining an XML namespace for your data contracts, defining the order of the data members etc. - but it works without adding any attributes.

So this is a known and willingly introduced behavior. I still believe you ought to be explicit in your intent and mark your data classes with [DataContract] and your members with [DataMember] (which also gives you the ability to ignore one or several public members and not include them in serialization).

不必在意 2024-10-04 20:34:42

根据,所有类型都可以序列化,而不仅仅是具有 DataContractAttribute 的序列化。

According to this all types can be serialized, not just ones with the DataContractAttribute.

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