C
C#12mo ago
Kroks

❔ Blazor TypeConverter Exception

[TypeConverter(typeof(IListStringConverter))]
public List<string> AcceptedDomains { get; set; } = new List<string>()
{
"discord",
"linktr.ee",
};
}
[TypeConverter(typeof(IListStringConverter))]
public List<string> AcceptedDomains { get; set; } = new List<string>()
{
"discord",
"linktr.ee",
};
}
<textarea placeholder="Domains" @bind=DataService.Filter.AcceptedDomains></textarea>
<textarea placeholder="Domains" @bind=DataService.Filter.AcceptedDomains></textarea>
11 Replies
Kroks
Kroks12mo ago
The type 'System.Collections.Generic.List1[[System.String, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]' does not have an associated TypeConverter that supports conversion from a string. Apply 'TypeConverterAttribute' to the type to register a converter."`
[TypeConverter(typeof(IListStringConverter))]
public class IListStringConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return sourceType == typeof(string);
}

public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
return destinationType == typeof(string);
}

public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string strValue)
{
string[] stringArray = strValue.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
return new List<string>(stringArray);
}

throw new NotSupportedException($"Cannot convert '{value}' to {typeof(IList<string>)}");
}

public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (value is IList<string> stringList)
{
return string.Join(",", stringList);
}

throw new NotSupportedException($"Cannot convert {typeof(IList<string>)} to '{destinationType}'");
}
}
[TypeConverter(typeof(IListStringConverter))]
public class IListStringConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return sourceType == typeof(string);
}

public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
return destinationType == typeof(string);
}

public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string strValue)
{
string[] stringArray = strValue.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
return new List<string>(stringArray);
}

throw new NotSupportedException($"Cannot convert '{value}' to {typeof(IList<string>)}");
}

public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (value is IList<string> stringList)
{
return string.Join(",", stringList);
}

throw new NotSupportedException($"Cannot convert {typeof(IList<string>)} to '{destinationType}'");
}
}
.
IsNotNull
IsNotNull12mo ago
I'm not super familiar with TypeConverter, but shouldn't the destination type be List<string> instead of string? I don't think it will use IListStringConverter if it doesn't support List<string>. Your CanConvertTo will return false for List<string>.
Kroks
Kroks12mo ago
changed but still doesnt work
Kroks
Kroks12mo ago
Kroks
Kroks12mo ago
with this it works
MODiX
MODiX12mo ago
Sorry @kroks1337 your message contained blocked content and has been removed!
Kroks
Kroks12mo ago
this seems dumb
Kroks
Kroks12mo ago
also I have another issue now, the IList<string> -> string works perfect but the binded List<string> does not get updated when changing text in the text area
Kroks
Kroks12mo ago
<textarea placeholder="Domains" @bind=DataService.Filter.AcceptedDomains></textarea>
IsNotNull
IsNotNull12mo ago
I am not finding documentation on using TypeConverter with Blazor, but there are a few third party articles and the GitHub issue where it was first implemented. According to the GitHub issue, you have to use TypeConverter with a class that is annotated with the TypeConverter and that overrides ToString. Inheriting from List<string> might work, but looks odd. What happens if you override ToString() on ConvertibleStringList and create the single string representation?
Accord
Accord12mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts