N
news.microsoft.com
Hi,
I'm trying to write TypeConverter which converts ArrayList to a
comma-separated string and back which can be stored in the attribute of
control's tag. In simplified form I have a property:
private ArrayList _test;
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[TypeConverter(typeof(ArrayListConverter))]
[NotifyParentProperty(true)]
public ArrayList Test
{
get
{
if (this._test == null)
this._test = new ArrayList();
return this._test;
}
set
{
this._test = value;
}
}
And a type converter class:
public class ArrayListConverter : TypeConverter
{
//
// skipped
//
public override object ConvertTo(ITypeDescriptorContext context,
System.Globalization.CultureInfo culture, object value, Type
destinationType)
{
if (value != null && !(value is ArrayList))
throw new ArgumentException("Invalid ArrayList object",
"value");
if (destinationType == typeof(string))
{
if (value == null)
return String.Empty;
ArrayList colors = (ArrayList)value;
string[] strColors = new string[colors.Count];
for (int i = 0; i < colors.Count; i++)
strColors = colors.ToString();
return String.Join(culture.TextInfo.ListSeparator,
strColors);
}
if (destinationType == typeof(InstanceDescriptor) && value is
ArrayList)
{
ArrayList clrs = (ArrayList)value;
ConstructorInfo cif = typeof(ArrayList).GetConstructor(new
Type[] { typeof(ICollection) });
if (cif != null)
return new InstanceDescriptor(cif, new object[] {
clrs }, false);
}
return base.ConvertTo(context, culture, value, destinationType);
}
//
// skipped
//
}
With this code I can edit the "Test" property in design mode via properties
window and as a string in the controls tag but when I try to build the site
I get "Object reference not set to an instance of an object" error. If I
change:
ConstructorInfo cif = typeof(ArrayList).GetConstructor(new
Type[] { typeof(ICollection) });
if (cif != null)
return new InstanceDescriptor(cif, new object[] {
clrs }, false);
to
ConstructorInfo cif =
typeof(ArrayList).GetConstructor(Type.EmptyTypes);
if (cif != null)
return new InstanceDescriptor(cif, null, false);
I don't get the error but tag is rendered to simple
@__ctrl.Test = new System.Collections.ArrayList();
which doesn't set anything from the attributes value.
What am I doing wrong?
Thank you,
Alan.
I'm trying to write TypeConverter which converts ArrayList to a
comma-separated string and back which can be stored in the attribute of
control's tag. In simplified form I have a property:
private ArrayList _test;
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[TypeConverter(typeof(ArrayListConverter))]
[NotifyParentProperty(true)]
public ArrayList Test
{
get
{
if (this._test == null)
this._test = new ArrayList();
return this._test;
}
set
{
this._test = value;
}
}
And a type converter class:
public class ArrayListConverter : TypeConverter
{
//
// skipped
//
public override object ConvertTo(ITypeDescriptorContext context,
System.Globalization.CultureInfo culture, object value, Type
destinationType)
{
if (value != null && !(value is ArrayList))
throw new ArgumentException("Invalid ArrayList object",
"value");
if (destinationType == typeof(string))
{
if (value == null)
return String.Empty;
ArrayList colors = (ArrayList)value;
string[] strColors = new string[colors.Count];
for (int i = 0; i < colors.Count; i++)
strColors = colors.ToString();
return String.Join(culture.TextInfo.ListSeparator,
strColors);
}
if (destinationType == typeof(InstanceDescriptor) && value is
ArrayList)
{
ArrayList clrs = (ArrayList)value;
ConstructorInfo cif = typeof(ArrayList).GetConstructor(new
Type[] { typeof(ICollection) });
if (cif != null)
return new InstanceDescriptor(cif, new object[] {
clrs }, false);
}
return base.ConvertTo(context, culture, value, destinationType);
}
//
// skipped
//
}
With this code I can edit the "Test" property in design mode via properties
window and as a string in the controls tag but when I try to build the site
I get "Object reference not set to an instance of an object" error. If I
change:
ConstructorInfo cif = typeof(ArrayList).GetConstructor(new
Type[] { typeof(ICollection) });
if (cif != null)
return new InstanceDescriptor(cif, new object[] {
clrs }, false);
to
ConstructorInfo cif =
typeof(ArrayList).GetConstructor(Type.EmptyTypes);
if (cif != null)
return new InstanceDescriptor(cif, null, false);
I don't get the error but tag is rendered to simple
@__ctrl.Test = new System.Collections.ArrayList();
which doesn't set anything from the attributes value.
What am I doing wrong?
Thank you,
Alan.