WCF中的自定义集合

WCF Tips之一

集合元素类的定义如下:
    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; }
        }

    }

自定义集合DocumentList则实现了IList<Document>接口:

注意,对于自定义集合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<Document>
    {}

此时,客户端应用程序可以直接使用数据契约,仍然能够识别。

原文地址:https://www.cnblogs.com/wayfarer/p/947095.html