|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
CollectionEditor and objects that implement an interfaceI am using Visual Studio 2005 (VB.NET) and I have encountered a problem that I am hoping someone can help me with. I am trying to get the CollectionEditor of a PropertyGrid control to allow the editing of a collection of objects that implement a custom interface (ITextTransform). The ITextTransform interface has a single method defined. [begin code] Public Interface ITextTransform Function Execute(ByVal Input As String) As String End Interface [end code] Classes that implement ITextTransform are used to transform an input string. Each class implements the Execute() method of ITextTransform and adds additional properties unique to the type of string transformation that is taking place (e.g A ReplaceText class adds Replace and ReplaceWith properties). I have created the following custom CollectionEditor class to only allow objects that implement the ITextTransform interface to be created. [begin code] Public Class CustomCollectionEditor Inherits CollectionEditor Public Sub New(ByVal newType As Type) MyBase.new(newType) End Sub ' Allow the Collection Editor to only create instances of classes that implement ' the ITextTransform interface, by using Reflection to dynamically build the allowed Type ' array. Protected Overrides Function CreateNewItemTypes() As System.Type() Dim ITextTransformType As Type = GetType(ITextTransform) Dim AllTypes As Type() = ITextTransformType.Assembly.GetTypes Dim AllowedNewTypes As New ArrayList For Each TempType As Type In AllTypes If ITextTransformType.IsAssignableFrom(TempType) = True And TempType.IsInterface = False Then _ AllowedNewTypes.Add(TempType) Next CreateNewItemTypes = AllowedNewTypes.ToArray(GetType(Type)) AllowedNewTypes = Nothing End Function Protected Overrides Function CanSelectMultipleInstances() As Boolean Return False End Function Protected Overrides Function GetDisplayText(ByVal value As Object) As String Return value.GetType.Name End Function End Class [end code] The property that exposes the collection of ITextTransform implemented objects in the class that is being bound to the PropertyGrid control has had the Editor attribute set, so that the CustomCollectionEditor class is used. [begin code] <Editor(GetType(CustomCollectionEditor), GetType(UITypeEditor))> _ Public ReadOnly Property Items() As List(Of ITextTransform) Get Return pItems End Get End Property [end code] When I open the collection editor and click the Add button, the object that is being created is of type ITextTransform, the interface, and *NOT* of the object that is implementing the interface. However, if I add a property declaration to the interface, and implement it in all classes that use the interface, then everything works as it should: [begin code] Public Interface ITextTransform Function Execute(ByVal Input As String) As String Property NonsenseProperty as Boolean End Interface [end code] While the code now works, I don't really want an unnecessary property like that. Is this a bug in the CollectionEditor code, that it cannot handle objects that implement interfaces if the interface does not have one or more properties defined? ...Or I am doing something really stupid? You can download the full code at http://rapidshare.com/files/65366563/CollectionEditiorProblem.zip.html Thanks for any help! David. |
|||||||||||||||||||||||