Custom set in WCF

Source: Internet
Author: User
One of the WCF tips

The collection element class is defined as follows: 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
{
Get {return m_filetype ;}
Set {m_filetype = value ;}
}

}

The custom set documentlist implements the ilist <document> interface:
// Which attribute shocould 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
}

Note that the [datacontract] feature cannot be applied to the custom set documentlist. Otherwise, the correct documentlist object cannot be returned in service operations. For example, the following service operation definition does not actually obtain the correct documentlist value: [Operationcontract]
[Faultcontract (typeof (directorynotfoundexception)]
Documentlist fetchdocuments (string homedir );

We should apply [collectiondatacontract] or [serializable] To the documentlist. The former is recommended. For a custom set, if it is a generic set, you can use the name attribute to specify the type name generated by the export metadata. However, for the set in this example, it doesn't matter because there is no generic parameter. To identify the element Document Type of the set when exporting metadata, you also need to apply knowtypeattribute. The final definition is modified as follows: [Knowntype (typeof (document)]
[Collectiondatacontract]
[Serializable]
Public class documentlist: ilist <document>
{}

At this time, the client application can directly use the data contract and still be recognized.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.