G
Guest
I’m successfully using the classes in the System.Configuration namespace to
strongly type my configuration objects.
However, I can’t see a mechanism by which I can use the configuration file
to indicate the *type* of object to create. Is this possible?
I really want to add a “Type†attribute to the configuration element like
this…
<configuration>
<configSections>
<section name="MySection" type="TestConfiguration.MySection,
TestConfiguration" />
</configSections>
<MySection>
<MyCollection>
<add Name="One" Type="1"/>
<add Name="Two" Type="2"/>
<add Name="Three" Type="3"/>
</MyCollection>
</MySection>
</configuration>
And then my code might look something like this (replacing the random
creation of objects with code driven by the configuration file)…
Sub Main()
Dim _MySection As MySection =
System.Configuration.ConfigurationManager.GetSection("MySection")
Dim _MyCollection As MyCollection = _MySection.MyCollection
For Each _MyElement As MyElement In _MyCollection
MsgBox(_MyElement.Name & " " & _MyElement.GetType.Name)
Next
End Sub
Friend Class MySection
Inherits System.Configuration.ConfigurationSection
<System.Configuration.ConfigurationProperty("MyCollection")> Friend
ReadOnly Property MyCollection() As MyCollection
Get
Return Me("MyCollection")
End Get
End Property
End Class
Friend Class MyCollection
Inherits System.Configuration.ConfigurationElementCollection
Protected Overloads Overrides Function CreateNewElement() As
System.Configuration.ConfigurationElement
'We need to read the "Type" attribute here so we know what class of
object to create!
Static _Random As New Random(Now.Second)
Select Case _Random.Next(1, 4)
Case 1
Return New MyElement1
Case 2
Return New MyElement2
Case 3
Return New MyElement3
End Select
End Function
Protected Overrides Function GetElementKey(ByVal element As
System.Configuration.ConfigurationElement) As Object
Return CType(element, MyElement).Name
End Function
End Class
Friend Class MyElement
Inherits System.Configuration.ConfigurationElement
<System.Configuration.ConfigurationProperty("Name")> Public ReadOnly
Property Name() As String
Get
Return Me("Name")
End Get
End Property
<System.Configuration.ConfigurationProperty("Type")> Public ReadOnly
Property Type() As String
Get
Return Me("Type")
End Get
End Property
End Class
Friend Class MyElement1
Inherits MyElement
End Class
Friend Class MyElement2
Inherits MyElement
End Class
Friend Class MyElement3
Inherits MyElement
End Class
strongly type my configuration objects.
However, I can’t see a mechanism by which I can use the configuration file
to indicate the *type* of object to create. Is this possible?
I really want to add a “Type†attribute to the configuration element like
this…
<configuration>
<configSections>
<section name="MySection" type="TestConfiguration.MySection,
TestConfiguration" />
</configSections>
<MySection>
<MyCollection>
<add Name="One" Type="1"/>
<add Name="Two" Type="2"/>
<add Name="Three" Type="3"/>
</MyCollection>
</MySection>
</configuration>
And then my code might look something like this (replacing the random
creation of objects with code driven by the configuration file)…
Sub Main()
Dim _MySection As MySection =
System.Configuration.ConfigurationManager.GetSection("MySection")
Dim _MyCollection As MyCollection = _MySection.MyCollection
For Each _MyElement As MyElement In _MyCollection
MsgBox(_MyElement.Name & " " & _MyElement.GetType.Name)
Next
End Sub
Friend Class MySection
Inherits System.Configuration.ConfigurationSection
<System.Configuration.ConfigurationProperty("MyCollection")> Friend
ReadOnly Property MyCollection() As MyCollection
Get
Return Me("MyCollection")
End Get
End Property
End Class
Friend Class MyCollection
Inherits System.Configuration.ConfigurationElementCollection
Protected Overloads Overrides Function CreateNewElement() As
System.Configuration.ConfigurationElement
'We need to read the "Type" attribute here so we know what class of
object to create!
Static _Random As New Random(Now.Second)
Select Case _Random.Next(1, 4)
Case 1
Return New MyElement1
Case 2
Return New MyElement2
Case 3
Return New MyElement3
End Select
End Function
Protected Overrides Function GetElementKey(ByVal element As
System.Configuration.ConfigurationElement) As Object
Return CType(element, MyElement).Name
End Function
End Class
Friend Class MyElement
Inherits System.Configuration.ConfigurationElement
<System.Configuration.ConfigurationProperty("Name")> Public ReadOnly
Property Name() As String
Get
Return Me("Name")
End Get
End Property
<System.Configuration.ConfigurationProperty("Type")> Public ReadOnly
Property Type() As String
Get
Return Me("Type")
End Get
End Property
End Class
Friend Class MyElement1
Inherits MyElement
End Class
Friend Class MyElement2
Inherits MyElement
End Class
Friend Class MyElement3
Inherits MyElement
End Class