WCF开发框架中有很多集合元素,在这里我们会为大家详细介绍这些WCF集合元素的定义方法,和相关使用技巧,希望对大家有所帮助。#t#

WCF集合元素类的定义如下:
WCF集合元素自定义集合DocumentList则实现了IList
- //which attribute should be applied here?
 - public class DocumentList:IList
 - {
 - private IList
 m_list = null; - public DocumentList()
 - {
 - m_list = new List
 (); - }
 - #region IList
 Members - public int IndexOf(Document item)
 - {
 - return m_list.IndexOf(item);
 - }
 - public void Insert(int index, Document item)
 - {
 - m_list.Insert(index,item);
 - }
 - public void RemoveAt(int index)
 - {
 - m_list.RemoveAt(index);
 - }
 - public Document this[int index]
 - {
 - get
 - {
 - return m_list[index];
 - }
 - set
 - {
 - m_list[index] = value;
 - }
 - }
 - #endregion
 - #region ICollection
 Members - public void Add(Document item)
 - {
 - m_list.Add(item);
 - }
 - public void Clear()
 - {
 - m_list.Clear();
 - }
 - public bool Contains(Document item)
 - {
 - return m_list.Contains(item);
 - }
 - public void CopyTo(Document[] array, int arrayIndex)
 - {
 - m_list.CopyTo(array,arrayIndex);
 - }
 - public int Count
 - {
 - get { return m_list.Count; }
 - }
 - public bool IsReadOnly
 - {
 - get { return m_list.IsReadOnly; }
 - }
 - public bool Remove(Document item)
 - {
 - return m_list.Remove(item);
 - }
 - #endregion
 - #region IEnumerable
 Members - public IEnumerator
 GetEnumerator() - {
 - return m_list.GetEnumerator();
 - }
 - #endregion
 - #region IEnumerable Members
 - IEnumerator IEnumerable.GetEnumerator()
 - {
 - return ((IEnumerable)m_list).GetEnumerator();
 - }
 - #endregion
 - }
 
注意,对于自定义集合DocumentList而言,我们不能应用[DataContract]特性,否则会在服务操作中无法返回正确的DocumentList对象。例如如下的服务操作定义,实际上无法获得正确的DocumentList值:
- [OperationContract]
 - [FaultContract(typeof
 
(DirectoryNotFoundException))]- DocumentList FetchDocuments
 
(string homeDir);
我们应该为DocumentList施加[CollectionDataContract]或者[Serializable],建议采用前者。因为对于自定义集合而言,如果是泛型集合,还可以利用Name属性制定导出元数据生成的类型名。不过,对于本例的集合而言,由于没有泛型参数,则无所谓了。为了在导出元数据时识别集合的元素Document类型,当然,还需要施加KnowTypeAttribute,最后的定义修改如下:
- [KnownType(typeof(Document))]
 - [CollectionDataContract]
 - [Serializable]
 - public class DocumentList:
 
IList- {}
 
此时,客户端应用程序可以直接使用数据契约,仍然能够识别WCF集合元素。
                当前名称:WCF集合元素基本概念详解
                
                浏览地址:http://www.csdahua.cn/qtweb/news23/346023.html
            
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网