Try making your item class derive from Component instead of being a
standalone root class. I have had code generation problems with collections
in the past and making sure your item class comes from the standard
Component base seems to solve most issues!
Phil Wright
Follow the microISV story at...
http://componentfactory.blogspot.com [quoted text, click to view] "news.microsoft.com" <x@x.com> wrote in message
news:OPGhZG1XFHA.3876@TK2MSFTNGP10.phx.gbl...
> Hi,
> I am a new .NET programmer starting with the Visual C# 2005 Express Beta
> 2, trying to replicate a VCL component that I use. I am trying to handle
> a collection of classes within the component, this class has its own
> properties that are editable by the standard collection editor. To break
> down the problem as much as possible, I have the following code:
>
> namespace mycomps {
>
> public partial class testcomp : UserControl
> {
> public class itemcollection : List<item>
> {
> public itemcollection() : base() { }
> public itemcollection(IEnumerable<item> collection) :
> base(collection) { }
> }
> private itemcollection FItems;
> public testcomp()
> {
> FItems = new itemcollection();
> InitializeComponent();
> }
>
> [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
> public itemcollection Items { get { return FItems; } }
> }
>
> [TypeConverter(typeof(item.converter))]
> public class item
> {
> private string FCaption;
> public item()
> {
> }
> public item(string caption)
> {
> FCaption = caption;
> }
> public class converter : TypeConverter
> {
> public override bool CanConvertTo(ITypeDescriptorContext context,
> Type destinationType)
> {
> if (destinationType ==
> typeof(System.ComponentModel.Design.Serialization.InstanceDescriptor))
> return true;
> return base.CanConvertTo(context, destinationType);
> }
> public override object ConvertTo(ITypeDescriptorContext context,
> System.Globalization.CultureInfo culture, object value, Type
> destinationType)
> {
> if (destinationType ==
> typeof(System.ComponentModel.Design.Serialization.InstanceDescriptor))
> {
> item value_item = (item)value;
> System.Reflection.ConstructorInfo ctor =
> typeof(item).GetConstructor(new Type[] { typeof(string) });
> return new
> System.ComponentModel.Design.Serialization.InstanceDescriptor(ctor, new
> object[] { value_item.FCaption });
> }
> return base.ConvertTo(context, culture, value,
> destinationType);
> }
> }
> public string Caption { get { return FCaption; } set { FCaption =
> value; } }
> }
>
> } // namespace mycomps
>
> When I place this component on a form, I can edit the Items property ok,
> adding new items and changing the Caption property. But when I hit ok and
> try to run it or look at the designer code I get the following in a
> message box:
>
> Code generation for property 'Items' failed. Error was: 'Unable to case
> object of type 'mycomps.item' to type 'mycomps.item'.'
>
> Thanks for any help that anyone can give,
>
> Steve
>