S
shapper
Hello,
I created a TypeConverter to convert a List<Tag> to a from String.
For example, List<Tag> to String all I need to do is:
String tags = MyListTags.ToString();
But how to make the inverse conversion? From String to List<Tag>?
I would need something like FromString ...
I am just not sure how to apply my converter in this case.
This is my Type Converter code:
public class TagsConverter : TypeConverter {
// CanConvertFrom
public override Boolean CanConvertFrom(ITypeDescriptorContext
context, Type sourceType) {
return sourceType == typeof(IList<Tag>) ? true :
base.CanConvertFrom(context, sourceType);
} // CanConvertFrom
// CanConvertTo
public override Boolean CanConvertTo(ITypeDescriptorContext
context, Type destinationType) {
return destinationType == typeof(String) ? true :
base.CanConvertTo(context, destinationType);
} // CanConvertTo
// ConvertFrom
public override Object ConvertFrom(ITypeDescriptorContext context,
CultureInfo culture, Object value) {
if (value is String) {
String tags = (String)value;
return tags.Split(new char[] { ',' },
StringSplitOptions.RemoveEmptyEntries).Select(t => new Tag() { Name =
t.Trim() }).ToList();
}
return base.ConvertFrom(context, culture, value);
}
// ConvertTo
public override Object ConvertTo(ITypeDescriptorContext context,
CultureInfo culture, Object value, Type destinationType) {
if (value is List<Tag>) {
List<Tag> tags = (List<Tag>)value;
return String.Join(", ", tags.Select(t => t.Name).ToArray());
}
return base.ConvertTo(context, culture, value, destinationType);
} // ConvertTo
} // TagsConverter
And I am registering it as follows:
TypeDescriptor.AddAttributes(typeof(List<Tag>), new
TypeConverterAttribute(typeof(TagsConverter)));
Thanks,
Miguel
I created a TypeConverter to convert a List<Tag> to a from String.
For example, List<Tag> to String all I need to do is:
String tags = MyListTags.ToString();
But how to make the inverse conversion? From String to List<Tag>?
I would need something like FromString ...
I am just not sure how to apply my converter in this case.
This is my Type Converter code:
public class TagsConverter : TypeConverter {
// CanConvertFrom
public override Boolean CanConvertFrom(ITypeDescriptorContext
context, Type sourceType) {
return sourceType == typeof(IList<Tag>) ? true :
base.CanConvertFrom(context, sourceType);
} // CanConvertFrom
// CanConvertTo
public override Boolean CanConvertTo(ITypeDescriptorContext
context, Type destinationType) {
return destinationType == typeof(String) ? true :
base.CanConvertTo(context, destinationType);
} // CanConvertTo
// ConvertFrom
public override Object ConvertFrom(ITypeDescriptorContext context,
CultureInfo culture, Object value) {
if (value is String) {
String tags = (String)value;
return tags.Split(new char[] { ',' },
StringSplitOptions.RemoveEmptyEntries).Select(t => new Tag() { Name =
t.Trim() }).ToList();
}
return base.ConvertFrom(context, culture, value);
}
// ConvertTo
public override Object ConvertTo(ITypeDescriptorContext context,
CultureInfo culture, Object value, Type destinationType) {
if (value is List<Tag>) {
List<Tag> tags = (List<Tag>)value;
return String.Join(", ", tags.Select(t => t.Name).ToArray());
}
return base.ConvertTo(context, culture, value, destinationType);
} // ConvertTo
} // TagsConverter
And I am registering it as follows:
TypeDescriptor.AddAttributes(typeof(List<Tag>), new
TypeConverterAttribute(typeof(TagsConverter)));
Thanks,
Miguel