I need convert string "foo1,foo2,foo3"
to string[]
.
I want to use TypeConverter
or its child ArrayConverter
. Its contain method ConvertFromString
.
But if I call this method I catch an exception ArrayConverter cannot convert from System.String.
I know about Split
, don't suggest me this solution.
----SOLUTION---
using advice @Marc Gravell and answer of this topic by @Patrick Hofman I wrote CustumTypeDescriptorProvider
public class CustumTypeDescriptorProvider:TypeDescriptionProvider
{
public override ICustomTypeDescriptor GetTypeDescriptor(System.Type objectType, object instance)
{
if (objectType.Name == "String[]") return new StringArrayDescriptor();
return base.GetTypeDescriptor(objectType, instance);
}
}
public class StringArrayDescriptor : CustomTypeDescriptor
{
public override TypeConverter GetConverter()
{
return new StringArrayConverter();
}
}
where StringArrayConverter
is implemented in answer below this post.
To use it I added CustumTypeDescriptorProvider
to collection of providers
TypeDescriptor.AddProvider(new CustumTypeDescriptorProvider(), typeof(string[]));
To use it in TestClass
you need to write a few lines:
TypeConverter typeConverter = TypeDescriptor.GetConverter(prop.PropertyType);
cValue = typeConverter.ConvertFromString(Value);
I believe that this can help somebody and save him from angry downvoiters.