|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Indicating the object type from app.configstrongly 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 Hello,
In the CreateNewElement() method, we are not able to get the item's attribute value before create it. So, I suggest we just create a MyElement object here, like: Protected Overloads Overrides Function CreateNewElement() As ConfigurationElement Return New MyElement() End Function And then you can get the Type information from the collection: For Each _MyElement As MyElement In _MyCollection MsgBox(_MyElement.Name & " " & _MyElement.Type) Next And then, create the actual type like "MyElement1" based on the Type information here. Sincerely, Luke Zhang Microsoft Online Community Support ================================================== Get notification to my posts through email? Please refer to http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif ications. Note: The MSDN Managed Newsgroup support offering is for non-urgent issues where an initial response from the community or a Microsoft Support Engineer within 1 business day is acceptable. Please note that each follow up response may take approximately 2 business days as the support professional working with you may need further investigation to reach the most efficient resolution. The offering is not appropriate for situations that require urgent, real-time or phone-based interactions or complex project analysis and dump analysis issues. Issues of this nature are best handled working with a dedicated Microsoft Support Engineer by contacting Microsoft Customer Support Services (CSS) at http://msdn.microsoft.com/subscriptions/support/default.aspx. ================================================== This posting is provided "AS IS" with no warranties, and confers no rights. |
|||||||||||||||||||||||