The configurationelementcollection. elementname attribute is interpreted in msdn as follows:
Gets the name used to identify this collection of elements in the configuration file When overridden in a derived class.
Obtains the name of this element set in the configuration file when the derived class is rewritten.
No sample code ...... I cannot understand it !!!
After research, we found that the function is similar to the configurationelementcollection. addelementname attribute (which must be set in configurationcollectionattribute), except that it is only for basicmap and basicmapalternate. We can also see from the name that addelementname, removeelementname, and clearelementname serve addremoveclearmap.
For example, the following simple code of the configurationelementcollection class cannot be simple:
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. configuration;
Namespace mgen
{
Class Program
{
Static void main ()
{
VaR mysec = (mysection) configurationmanager. getsection ("mysec ");
Foreach (myelemenet ele in mysec. myelements)
Console. writeline (Ele. ID );
}
}
Class mysection: configurationsection
{
[Configurationproperty ("", isdefaultcollection = true)]
Public mycollec myelements
{
Get {return (mycollec) This [""];}
}
}
[Configurationcollection (typeof (myelemenet), additemname = "ele")]
Class mycollec: configurationelementcollection
{
Protected override configurationelement createnewelement ()
{
Return new myelemenet ();
}
Protected override object getelementkey (configurationelement element)
{
Return (myelemenet) element). ID;
}
}
Class myelemenet: configurationelement
{
[Configurationproperty ("ID", iskey = true)]
Public int ID
{
Get {return (INT) This ["ID"];}
}
[Configurationproperty ("name")]
Public string name
{
Get {return (string) This ["name"];}
}
}
}
Because addelementname is set, we can write it in the configuration file as follows:
<? XML version = "1.0" encoding = "UTF-8"?>
<Configuration>
<Configsections>
<Section name = "mysec" type = "mgen. mysection, mgen"/>
</Configsections>
<Mysec>
<Ele id = "23" name = "mgen"/>
<Ele id = "34" name = "another"/>
</Mysec>
</Configuration>
Output: 23 and 34
In this case, the configurationelementcollection type is addremoveclearmap by default. After the configurationelementcollection type is changed to basicmap or basicmapalternate, addelementname cannot be used, and an exception is thrown (the unrecognized element 'ele ').
In this case, you do not need to set addelementname to rewrite the elementname attribute.
[Configurationcollection (typeof (myelemenet)]
Class mycollec: configurationelementcollection
{
Public override configurationelementcollectiontype collectiontype
{
Get
{
Return configurationelementcollectiontype. basicmap;
}
}
Protected override string elementname
{
Get
{
Return "ele ";
}
}
Protected override configurationelement createnewelement ()
{
Return new myelemenet ();
}
Protected override object getelementkey (configurationelement element)
{
Return (myelemenet) element). ID;
}
}