Implementing custom configuration section handler

前端 未结 1 1811
时光取名叫无心
时光取名叫无心 2021-01-24 16:24

gleanings from a variety of sources (including stackOverlflow), however when I come to use it, I get the following error message

\"The Configuration property \'devicecon

相关标签:
1条回答
  • 2021-01-24 16:48

    The DeviceConfig property is not needed at the top level. What you do need to do is add a property for your ConfigurationElementCollection devices. I would also rename your Devices class something a little less ambiguous, say, DeviceElementCollection. Try this:

    Public Class DeviceConfig    
        Inherits ConfigurationSection    
    
        <ConfigurationProperty("Devices")> _    
        Public ReadOnly Property Devices() As DeviceElementCollection    
            Get    
                Return CType(Me("Devices"), DeviceElementCollection)    
            End Get    
        End Property    
    End Class    
    

    Then, in your definition for DeviceElementCollection, you may have problems if you don't add the following:

    Public Overrides ReadOnly Property CollectionType() As System.Configuration.ConfigurationElementCollectionType
        Get
            Return ConfigurationElementCollectionType.BasicMap
        End Get
    End Property
    
    Protected Overrides ReadOnly Property ElementName() As String
        Get
            Return "Device"
        End Get
    End Property
    

    I wrote up a pretty long answer to a similar question (sorry, in C#) here.

    Update - how to use in code

    Dim deviceConfiguration as DeviceConfig = ConfigurationManager.GetSection("deviceconfigs")
    For Each device As Device in deviceConfiguration.Devices
        '...whatever you need to do
    Next
    
    0 讨论(0)
提交回复
热议问题