zoukankan      html  css  js  c++  java
  • 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>
        {}

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

  • 相关阅读:
    Android——点击对话框上按钮不关闭对话框
    超酷的Android 侧滑(双向滑动菜单)效果
    Android快速开发不可或缺的11个工具类(下载)
    Android ——真机调试
    Android程序完全退出的三种方法
    android 添加桌面快捷方式
    最全Android开发常用工具类
    成为Java GC专家(5)—Java性能调优原则
    JVM调优总结 + jstat 分析
    mysql中max_allowed_packet参数的配置方法(避免大数据写入或者更新失败)
  • 原文地址:https://www.cnblogs.com/wayfarer/p/947095.html
Copyright © 2011-2022 走看看