Guest User

Untitled

a guest
Aug 17th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. Accessing grouped settings in a config file but NOT the app.config
  2. public class CustomFieldElement : ConfigurationElement
  3. {
  4. [ConfigurationProperty("fieldName", DefaultValue = "", IsKey = true, IsRequired = true)]
  5. public string fieldName
  6. {
  7. get { return (string)base["fieldName"]; }
  8. set { base["fieldName"] = value; }
  9. }
  10. [ConfigurationProperty("fieldType", DefaultValue = "", IsKey = true, IsRequired = true)]
  11. public string fieldType
  12. {
  13. get { return (string)base["fieldType"]; }
  14. set { base["fieldType"] = value; }
  15. }
  16.  
  17. [ConfigurationProperty("xpath", DefaultValue = "", IsKey = true, IsRequired = true)]
  18. public string xpath
  19. {
  20. get { return (string)base["xpath"]; }
  21. set { base["xpath"] = value; }
  22. }
  23. }
  24.  
  25. [ConfigurationCollection(typeof(CustomFieldElement))]
  26. public class FieldAppearanceCollection : ConfigurationElementCollection
  27. {
  28. internal const string PropertyName = "FieldDescription";
  29.  
  30. public override ConfigurationElementCollectionType CollectionType
  31. {
  32. get
  33. {
  34. return ConfigurationElementCollectionType.BasicMapAlternate;
  35. }
  36. }
  37. protected override string ElementName
  38. {
  39. get
  40. {
  41. return PropertyName;
  42. }
  43. }
  44.  
  45. protected override bool IsElementName(string elementName)
  46. {
  47. return elementName.Equals(PropertyName, StringComparison.InvariantCultureIgnoreCase);
  48. }
  49.  
  50.  
  51. public override bool IsReadOnly()
  52. {
  53. return false;
  54. }
  55.  
  56.  
  57. protected override ConfigurationElement CreateNewElement()
  58. {
  59. return new CustomFieldElement();
  60. }
  61.  
  62. protected override object GetElementKey(ConfigurationElement element)
  63. {
  64. return ((CustomFieldElement)(element)).fieldName;
  65. }
  66.  
  67. public CustomFieldElement this[int idx]
  68. {
  69. get
  70. {
  71. return (CustomFieldElement)BaseGet(idx);
  72. }
  73. }
  74. }
  75.  
  76. public class ConnectionSection : ConfigurationSection
  77. {
  78. [ConfigurationProperty("fields")]
  79. public FieldAppearanceCollection FieldElement
  80. {
  81. get { return ((FieldAppearanceCollection)(base["fields"])); }
  82. set { base["fields"] = value; }
  83. }
  84. }
  85.  
  86. public class ConfigSettings
  87. {
  88. public ConnectionSection CustomFieldAppearanceConfiguration
  89. {
  90. get
  91. {
  92. return (ConnectionSection)ConfigurationManager.GetSection("customFields");
  93. }
  94. }
  95.  
  96. public FieldAppearanceCollection CustomFieldApperances
  97. {
  98. get
  99. {
  100. return this.CustomFieldAppearanceConfiguration.FieldElement;
  101. }
  102. }
  103.  
  104. public IEnumerable<CustomFieldElement> CustomFieldElements
  105. {
  106. get
  107. {
  108. foreach (CustomFieldElement selement in this.CustomFieldApperances)
  109. {
  110. if (selement != null)
  111. yield return selement;
  112. }
  113. }
  114. }
  115. }
Add Comment
Please, Sign In to add comment