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.