سؤال

يتجول من مجموعة متنوعة من المصادر (بما في ذلك stackoverlflow) ، ولكن عندما أتوصل إلى استخدامها ، أحصل على رسالة الخطأ التالية

"لا يجوز اشتقاق خاصية التكوين" deviceConfig "من التكوين."

لقد كنت أعاني من ذلك من أجل الجزء الأفضل من يوم واحد الآن ، وأنا لست بالقرب من الحل ، لذلك سيكون موضع تقدير كبير في أي مساعدة في هذا الأمر. أنا مبتدئ في تنفيذ أقسام التكوين المخصصة ، لذا يرجى معاملتي بلطف :-)

يبدو ملف app.config هكذا:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="deviceconfig"  type="CNIMonitor.Core.CustomConfig.DeviceConfig,Core"/>
  </configSections>
<system.diagnostics>
    <sources>
        <!-- This section defines the logging configuration for My.Application.Log -->
        <source name="DefaultSource" switchName="DefaultSwitch">
            <listeners>
                <add name="FileLog"/>
                <!-- Uncomment the below section to write to the Application Event Log -->
                <!--<add name="EventLog"/>-->
            </listeners>
        </source>
    </sources>
    <switches>
        <add name="DefaultSwitch" value="Information" />
    </switches>
    <sharedListeners>
        <add name="FileLog"
             type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" 
             initializeData="FileLogWriter"/>
        <!-- Uncomment the below section and replace APPLICATION_NAME with the name of your application to write to the Application Event Log -->
        <!--<add name="EventLog" type="System.Diagnostics.EventLogTraceListener" initializeData="APPLICATION_NAME"/> -->
    </sharedListeners>
</system.diagnostics>
<deviceconfig>
  <devices>          
      <device deviceid = "1" 
              name = "localhost" 
              ipaddress="127.0.0.1" 
              port="10000"
              status="not listening"
              message="no message"
      />
      <device deviceid ="2" 
              name ="2nd localhost" 
              ipaddress="127.0.0.1" 
              port="20000" 
              status="not listening"
              message="no message"
      />
  </devices>  
</deviceconfig>

رمز معالج القسم المخصص على النحو التالي:

Imports System.Configuration

مساحة الاسم cnimonitor.core.customConfig

Public Class DeviceConfig
    Inherits ConfigurationSection
    <ConfigurationProperty("deviceconfig")> _
    Public ReadOnly Property DeviceConfig() As DeviceConfig
        Get
            Return CType(MyBase.Item("deviceconfig"), DeviceConfig)
        End Get
    End Property
End Class

<ConfigurationCollectionAttribute(GetType(Device))> _
Public Class Devices
    Inherits ConfigurationElementCollection
    Protected Overrides Function CreateNewElement() As ConfigurationElement
        Return New Device
    End Function
    Protected Overrides Function GetElementKey _
    (ByVal element As ConfigurationElement) As Object
        Return CType(element, Device).DeviceID
    End Function
    Public Sub Add(ByVal element As Device)
        Me.BaseAdd(element)
    End Sub
End Class
Public Class Device
    Inherits ConfigurationElement
    <ConfigurationProperty("deviceid", DefaultValue:="", IsKey:=True, IsRequired:=True)> _
    Public Property DeviceID() As String
        Get
            Return CType(MyBase.Item("deviceid"), String)
        End Get
        Set(ByVal value As String)
            MyBase.Item("deviceid") = value
        End Set
    End Property
    <ConfigurationProperty("hostname", DefaultValue:="", IsKey:=True, IsRequired:=False)> _
    Public Property HostName() As String
        Get
            Return CType(MyBase.Item("RegisteredDate"), String)
        End Get
        Set(ByVal value As String)
            MyBase.Item("RegisteredDate") = value
        End Set
    End Property
    <ConfigurationProperty("ipaddress", DefaultValue:="", IsKey:=True, IsRequired:=False)> _
    Public Property IpAddress() As String
        Get
            Return CType(MyBase.Item("ipaddress"), String)
        End Get
        Set(ByVal value As String)
            MyBase.Item("ipaddress") = value
        End Set
    End Property
    <ConfigurationProperty("port", DefaultValue:="", IsKey:=True, IsRequired:=False)> _
    Public Property Port() As String
        Get
            Return CType(MyBase.Item("port"), String)
        End Get
        Set(ByVal value As String)
            MyBase.Item("port") = value
        End Set
    End Property
    <ConfigurationProperty("status", DefaultValue:="", IsKey:=False, IsRequired:=False)> _
    Public Property Status() As String
        Get
            Return CType(MyBase.Item("status"), String)
        End Get
        Set(ByVal value As String)
            MyBase.Item("status") = value
        End Set
    End Property
    <ConfigurationProperty("message", DefaultValue:="", IsKey:=False, IsRequired:=False)> _
    Public Property Message() As String
        Get
            Return CType(MyBase.Item("message"), String)
        End Get
        Set(ByVal value As String)
            MyBase.Item("message") = value
        End Set
    End Property
End Class

End Namespace
هل كانت مفيدة؟

المحلول

ليس هناك حاجة إلى خاصية DeviceConfig في المستوى الأعلى. ماذا أنت فعل الحاجة إلى القيام به هو إضافة خاصية لك ConfigurationElementCollection الأجهزة. أود أيضا إعادة تسمية الخاص بك Devices فئة شيء أقل غموضا ، قل ، DeviceElementCollection. جرب هذا:

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    

ثم ، في تعريفك ل DeviceElementCollection, ، قد تواجه مشاكل إذا لم تضيف ما يلي:

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

لقد كتبت إجابة طويلة جدًا على سؤال مماثل (آسف ، في C#) هنا.

تحديث - كيفية استخدام الكود

Dim deviceConfiguration as DeviceConfig = ConfigurationManager.GetSection("deviceconfigs")
For Each device As Device in deviceConfiguration.Devices
    '...whatever you need to do
Next
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top