Custom Control with Custom Collection Property
I have an ASPX Custom Control which is supposed to load it's prop开发者_JAVA百科erties into an internal collection (defined with PersistenceMode.InnerProperty). Here's the ASPX
<cc:CustomControl runat="server">
<Queries>
<cc:QueryTypeOne ... />
</Queries>
</cc:CustomControl>
The problem is, when I use the above code, I get a parser error "Type 'CustomControls.QueryCollection' does not have a public property named 'QueryTypeOne'". (FYI QueryTypeOne inherits a class called Query). I don't want a public property called QueryTypeOne, I want it to load into the QueryCollection property I have waiting for it!
Here's all the relevant code.
CustomControls.CustomControl class has a property called queries
<DefaultValue(CType(Nothing, String)),
MergableProperty(False),
PersistenceMode(PersistenceMode.InnerProperty)> _
Public Property Queries As QueriesContainerTag
And QueriesContainerTag just exists to allow the ASPX engine to get access to the collection (QueryCollection)
<ParseChildren(True, "Queries"),
Serializable()> _
Public Class QueriesContainerTag
Private _Queries As QueryCollection = Nothing
Public ReadOnly Property Queries As QueryCollection
Get
If _Queries Is Nothing Then
_Queries = New QueryCollection()
End If
Return _Queries
End Get
End Property
End Class
Where QueryCollection is a custom class that implements ICollection with this signature:
Public Class QueryCollection
Implements IList(Of Query), ICollection(Of Query), IEnumerable(Of Query)
Also, oddly enough, if I changed QueriesContainerTag to use a built in list class, everything works perfectly!
<ParseChildren(True, "Queries"),
Serializable()> _
Public Class QueriesContainerTag
Private _Queries As List(Of Query) = Nothing
Public ReadOnly Property Queries As List(Of Query)
Get
If _Queries Is Nothing Then
_Queries = New List(Of Query)()
End If
Return _Queries
End Get
End Property
End Class
Microsoft does not implement Generic List support for custom control collection properties! As soon as I added the IList
interface, everything started working exactly as expected.
Personally, I don't understand why they did this. Even more, I would have thought an IList(Of T)
(IList<T>
for you C# guys) should work automatically everywhere an IList
is required. After all, T will always inherit from object.
精彩评论