. Net (C #) configure the system-configurationelementcollection. elementname attribute

Source: Internet
Author: User

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;

}

}

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.