我可以添加一个WCF DataContract复杂类型?复杂、类型、WCF、DataContract

2023-09-03 02:53:36 作者:划破寂寞的天空、

我有一个复杂的数据类型,包括许多功能,以及通常的获取和得到的方法。我的生活会容易得多,如果我可以使用WCF所以我的客户也可以使用此数据类型。

I have a complex data type, including a number of functions, as well as the usual get and get methods. My life would be considerably easier if I could use WCF so my client can also use this data type.

忽略所有的操作,把 [DataMemeber] 只在需要的地方。

把类的问题,在一个共享库组件,用于客户端和服务器的访问。

Put the class in question in a shared library assembly for both client and server to access.

谢谢, 罗伯托

PS。我意识到问题可能没有得到很好的措辞,因为它可以。

PS. I realise that the question is probably not as well worded as it could be.

推荐答案

好吧,这原来是上述所有答案的组合。

Ok, it turns out to be a combination of all the above answers.

坚持的数据类到一个共享组件,从客户端和服务器项目引用。 确保您在配置服务引用对话框中选中项目中引用的程序集重用类型。 在您的每一个数据的合同的开头放一个[KnownType]属性。

在code看起来像这样:

The code looks like so:

[DataContract]
[KnownType(typeof(WHS2SmugmugShared.Photo))]
[KnownType(typeof(WHS2SmugmugShared.PhotoInfo))]
public class Photo
{
//code here
}

在上述情况下,我在照片类使用PhotoInfo。 PhotoInfo没有与它的类文件关联的KnownType属性。并且它似乎没有被要求。

In the above case, I use PhotoInfo in the Photo class. PhotoInfo does NOT have a KnownType attribute associated with it in the class file. And it does not appear to be required.

这允许你序列化复杂类型,但仍保持其业务。

This allows you to serialize complex types but still keep their operations.

 
精彩推荐
图片推荐