当我们在使用WCF自定义集合类型当做服务契约发布的时候,需要注意很多问题。在这篇文章中就总结了一些注意事项,希望对大家有些帮助。#t#
1.WCF自定义集合类型必须使用[Serializable]和[DataContract]标记;
2.集合包含的类型属性必须使用 [DataMember]标记,并且,如果是属性(Property),必须要实现get和set;
3.集合类型必须使用[Serializable]和[CollectionDataContract]标记,以及[KnownType]标记指向集合包含的子类型;
4.集合类型必须实现IEnumerable<T>接口;
5.WCF自定义集合类型使用[DataMember]标记的IList将集合项向客户端公开.
样例如下:
namespace Sharpnessdotnet
{
[Serializable]
[DataContract]
public class Sharpnessdotnet
{
private string name;
[DataMember]
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
}
[Serializable]
[CollectionDataContract]
[KnownType(typeof(Sharpnessdotnet))]
public class SharpnessdotentCollection :
IEnumerable<Sharpnessdotnet>
{
[DataMember]
public IList<Sharpnessdotnet> List;
public SharpnessdotentCollection()
{
List = new List<Sharpnessdotnet>();
}
public void Add(Sharpnessdotnet obj)
{
List.Add(obj);
}
public IEnumerator<Sharpnessdotnet>
GetEnumerator()
{
return List.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return List.GetEnumerator();
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
以上就是WCF自定义集合类型相关概念总结。