Answer the question
In order to leave comments, you need to log in
Can the .Configure() method configure other types than the standard ones?
There is a class with methods: Emote.Parse(string) - returns Emote, Emote.TryParse(string) - returns bool, etc.
There is a parameter class:
public class EmotesOptions
{
public Emote CheckMark { get; set; }
public Emote Clock { get; set; }
}
public void ConfigureServices(IServiceCollection services)
{
services.Configure<EmotesOptions>(Configuration.GetSection("Emotes");
}
Answer the question
In order to leave comments, you need to log in
I found the answer to the question: Let's
use Type Converter.
Let's create a class EmotesConverter dependent on TypeConverter with the following content:
public class EmotesConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return sourceType == typeof(string) ||
base.CanConvertFrom(context, sourceType);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
return value is string emoteString
? Emote.Parse(emoteString)
: base.ConvertFrom(context, culture, value);
}
}
[TypeConverter(typeof(EmotesConverter))]
public class EmotesOptions
{
public Emote CheckMark { get; set; }
public Emote Clock { get; set; }
}
public void ConfigureServices(IServiceCollection services)
{
TypeDescriptor.AddAttributes(typeof(Emote), new TypeConverterAttribute(typeof(EmotesConverter)));
services.Configure<EmotesOptions>(Configuration.GetSection("Emotes");
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question