I have a control that has a property of type ControlProp. ControlProp class has a member of type IList<PropType>. Each PropType has a member of type IList<PropTypeNested>.
I have defined type converter for the PropType below to enable generation of constructor for the PropType. Also note that PropTypeNested is marked as "Serializable"
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
IList<PropTypeNested> nestedPropTypeList = new List<PropTypeNested>();
nestedPropTypeList.Add(new PropTypeNested(10));
controlProp.PropTypeList.Add(new PropType(nestedPropTypeList));
}
private ControlProp controlProp = new ControlProp();
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public ControlProp ControlProp
{
get
{
return controlProp;
}
set
{
controlProp = value;
}
}
}
public class ControlProp
{
private IList<PropType> propTypeList = new List<PropType>();
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public IList<PropType> PropTypeList
{
get { return propTypeList; }
set { propTypeList = value; }
}
}
[TypeConverter(typeof(PropTypeConverter))]
public class PropType
{
private IList<PropTypeNested> propTypeNestedList;
public IList<PropTypeNested> PropTypeNestedList
{
get { return propTypeNestedList; }
set { propTypeNestedList = value; }
}
public PropType(IList<PropTypeNested> propTypeNestedList)
{
this.propTypeNestedList = propTypeNestedList;
}
}
[Serializable]
public class PropTypeNested
{
private int x;
public int X
{
get { return x; }
set { x = value; }
}
public PropTypeNested(int x)
{
this.x = x;
}
}
public class PropTypeConverter : TypeConverter
{
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(InstanceDescriptor))
{
return true;
}
else
{
return base.CanConvertTo(context, destinationType);
}
}
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(InstanceDescriptor) && value is PropType)
{
PropType propType = (PropType)value;
ConstructorInfo ctor = typeof(PropType).GetConstructor
(
new Type[]
{
typeof(IList<PropTypeNested>),
}
);
return new InstanceDescriptor(ctor, new object[]
{
propType.PropTypeNestedList,
});
}
else
{
return base.ConvertTo(context, culture, value, destinationType);
}
}
}
The problem is that VS generates code below:
controlProp1.PropTypeList.Add(new DesignerTest.PropType(((System.Collections.Generic.IList<DesignerTest.PropTypeNested>)(resources.GetObject("controlProp1.PropTypeList")))));
Is there a way to generate the code without the binary serialization for PropTypeNested list. I want to generate a code similar to the one below and bypass binary serialization. I have spent several hours trying to resolve it and appreciate any help.
IList<PropTypeNested> nestedPropTypeList = new List<PropTypeNested>();
nestedPropTypeList.Add(new PropTypeNested(10));
controlProp.PropTypeList.Add(new PropType(nestedPropTypeList));