Skip to content

2. Support for data types

Emrys edited this page Jan 25, 2018 · 1 revision

1、simple data types

Common data types support String, Int, Double, DateTime, Bool, Enum and so on. These are simple data types directly used.

1.1、note that these values can be set in Attribute settings in the Xml configuration file

<userInfo 
    userName='Emrys' 
    email='i@emrys.me' 
    age='27' 
    blogUrl='http://www.cnblogs.com/emrys5/' 
    favoriteColor='Blue'
    dislikeColor='2'
></userInfo>

1.2、these values can also be set in Element

<userInfo>
    <userName>Emrys</userName>
    <email>i@emrys.me</email>
    <age>27</age>
    <blogUrl>http://www.cnblogs.com/emrys5/</blogUrl>
    <favoriteColor>Blue</favoriteColor>
    <dislikeColor>2</dislikeColor> 
</userInfo>

of course, can be mixed, so it can be conveniently configured

2、complex type

2.1、KeyValuePair data type

2.1.1 Config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="sport" type="Emrys.SuperConfig.Section,Emrys.SuperConfig"></section>
  </configSections>
  <sport>
    <key>2</key>
    <value>Football</value>
  </sport>
</configuration>

2.1.2 Get value

  var sport = SuperConfig.Mapping<KeyValuePair<int, string>>("sport"); 
  Assert.AreEqual(sport.Key, 2);
  Assert.AreEqual(sport.Value, "Football"); 

2.1.3 remark

  1. in the configuration config, the names of key and value are not the same as the values set in the ConvertCase in the Setting, and the default is key and value.
  2. also key value also supports setting values in Attribute
 <sport key="2" value="Football"> 
 </sport>

2.2、List和Array data type

The usage of List and Array is similar to the configuration, so here is only the use of List.

2.2.1 Config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections> 
    <section name="listString" type="Emrys.SuperConfig.Section,Emrys.SuperConfig"></section> 
  </configSections> 
  <listString>
    <item>a</item>
    <item>b</item>
    <item>c</item>
    <item>d</item>
  </listString>  
</configuration>

2.2.2 Get value

var listString = SuperConfig.Mapping<List<string>>("listString"); 
Assert.AreEqual(listString.First(), "a");
Assert.AreEqual(listString.Count, 4);

2.2.3 remark

  1. in List, subitems must be configured with Element, and the name can be customized.
  2. in List, it can contain simple types, such as String, Int, Double, DateTime, Bool, Enum, etc., and can also contain entity objects. Of course, List can be included as long as SuperConfig supports, such as:
<family>
  <item userName='lcz' email='xxx@qq.com' age='50'></item>
  <item>
    <userName>ly</userName>
    <email>ly@qq.com</email>
    <age>30</age>
  </item>
</family> 

2.3、Dictionary data type

2.3.1 Config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections> 
    <section name="dictionaryIntString" type="Emrys.SuperConfig.Section,Emrys.SuperConfig"></section> 
  </configSections> 
  <dictionaryIntString>
    <item key="1" value="a"></item>
    <item>
      <key>2</key>
      <value>b</value>
    </item>
  </dictionaryIntString> 
</configuration>

2.3.2 Get value

var dic = SuperConfig.Mapping<Dictionary<int, string>>("dictionaryIntString");
Assert.AreEqual(dic.First().Key, 1);
Assert.AreEqual(dic.Last().Value, "b");

2.3.3 Remark

  1. in Dictionary, the t name of key and value is the same as the values set by ConvertCase in Setting, and the default is key and value.
  2. in Dictionary, it can contain simple types, such as String, Int, Double, DateTime, Bool, Enum, etc., and can also contain entity objects. Of course, List can be included as long as SuperConfig supports, such as:
<colleagues>
  <colleague>
    <key>1</key>
    <value>
      <userName>zfx</userName>
      <email>wy@qq.com</email>
      <age>100</age>
    </value>
  </colleague>
  <colleague>
    <key>2</key>
    <value>
      <userName>zk</userName>
      <email>zk@qq.com</email>
      <age>30</age>
    </value>
  </colleague>
</colleagues>