[.NET] Custom defined Attribute Configuration read section

[.NET]: Custom Configuration undefined Attribute reading and writing sections



Preface:

When we write custom Configuration, we will first define the corresponding custom ConfigurationSection good and Config files let the program know quite how to analyze the data.



  
  
  
   
   
    
    
    
  
   
    
       

  
  


public class SampleSection : ConfigurationSection
{
    [ConfigurationProperty("propertyA")]
    public string PropertyA
    {
        get
        {
            return (string)base["propertyA"];
        }
        set
        {
            base["propertyA"] = value;
        }
    }

    [ConfigurationProperty("propertyCollectionB")]
    public NameValueConfigurationCollection PropertyCollectionB
    {
        get
        {
            return (NameValueConfigurationCollection)base["propertyCollectionB"];
        }
    }  
}

But when we Config file the following XML, which propertyC the Attribute is not fixed when the number of names.
The practice of the above methods can not meet this demand.



  
  
  
   
   
    
    
    
  
   
    
   
  
   
   

  
   
  

This article describes how to use the program to accomplish this demand.


Note:

In the Microsoft Visual Studio IDE debug environment, the implementation of a program written in the .config.
Program will be written * .vshost.exe.config, instead of the expected * .exe.config.
And after the end of program execution, IDE will cover * .vshost.exe.config a state before writing.
So it will mistakenly believe that the program fails.


After compiling just finished,
click the bin directory under the * .exe, go check * .exe.config, you can see the results expected.


related data :

[ASP.NET] write their own Configuration section 1 Part: http://www.dotblogs.com.tw/regionbbs/archive/2009/05/08/custom_configuration_section_in_web_config.aspx
[ASP.NET] write your own Configuration section 2 Part: http://www.dotblogs.com.tw/regionbbs/archive/2009/09/17/orgainzeyoursectionintogroupforconfiguration.aspx
[ASP.NET] write their own Configuration section of Part 3: http: //www.dotblogs. com.tw/regionbbs/archive/2009/10/09/customconfigurationelementcollection.aspx
[.NET]: custom Configuration data segment written: http://www.dotblogs.com.tw/clark/archive/2010/ 12/23 / 20338.aspx


Practice:

Download: Clk.ConfigurationSample2.zip


Add items or references UnrecognizedAttributeConfigurationElement, UnrecognizedAttributeDictionary
image
image


自订ConfigurationSection
image


Set .Config
image


Writer
image


Results of the
image
image


Postscript:

Custom Configuration section, use UnrecognizedAttributeConfigurationElement, UnrecognizedAttributeDictionary.
Undefined Attribute reading to do with writing is actually quite easy: D.

Signature
hopes he
can in a more simple language and procedures, to convey the spirit behind the design of the program.
Truly "to write to God" realm.

Original: Large column  [.NET] Custom Configuration undefined Attribute reading and writing sections


Guess you like

Origin www.cnblogs.com/chinatrump/p/11518137.html