集合元素类的定义如下:
public
enum
FileType
{
TXT,DOC,HTML,OTHER
}
[DataContract]
public class Document
{
private string m_localPath;
private string m_fileName;
private FileType m_fileType;
[DataMember]
public string LocalPath
{
get { return m_localPath; }
set { m_localPath = value; }
}
[DataMember]
public string FileName
{
get { return m_fileName; }
set { m_fileName = value; }
}
[DataMember]
public FileType FileType
{
get { return m_fileType; }
set { m_fileType = value; }
}
}
{
TXT,DOC,HTML,OTHER
}
[DataContract]
public class Document
{
private string m_localPath;
private string m_fileName;
private FileType m_fileType;
[DataMember]
public string LocalPath
{
get { return m_localPath; }
set { m_localPath = value; }
}
[DataMember]
public string FileName
{
get { return m_fileName; }
set { m_fileName = value; }
}
[DataMember]
public FileType FileType
{
get { return m_fileType; }
set { m_fileType = value; }
}
}
自定义集合DocumentList则实现了IList<Document>接口:
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
//which attribute should be applied here?
public class DocumentList:IList<Document>
{
private IList<Document> m_list = null;
public DocumentList()
{
m_list = new List<Document>();
}
#region IList<Document> 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<Document> 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<Document> Members
public IEnumerator<Document> 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);
[FaultContract( typeof (DirectoryNotFoundException))]
DocumentList FetchDocuments( string homeDir);
我们应该为DocumentList施加[CollectionDataContract]或者[Serializable],建议采用前者。因为对于自定义集合而言,如果是泛型集合,还可以利用Name属性制定导出元数据生成的类型名。不过,对于本例的集合而言,由于没有泛型参数,则无所谓了。为了在导出元数据时识别集合的元素Document类型,当然,还需要施加KnowTypeAttribute,最后的定义修改如下:
[KnownType(
typeof
(Document))]
[CollectionDataContract]
[Serializable]
public class DocumentList:IList < Document >
{
}
[CollectionDataContract]
[Serializable]
public class DocumentList:IList < Document >
{
![dot.gif](/Images/dot.gif)
此时,客户端应用程序可以直接使用数据契约,仍然能够识别。