I'm searching for some way to implement some TypeConverter in order to convert a comma separated string to some generic array of enum values.
var input = "Value01, Value02";
var output = new CustomEnum[] { CustomEnum.Value01, CustomEnum.Value02 };
I'm using
class StringToEnumList : ITypeConverter<string, Enum[]> ...
cfg.CreateMap<string, Enum[]>().ConvertUsing<StringToEnumList>(); ...
But this option only convert explicit Enum property types not any custom Enum property.
enum CustomEnum { Value01, Value02 }
class MyClassSource {
public string OkProperty {get; set; }
public string FailingProperty {get; set; }
}
class MyClassTarget {
public Enum[] OkProperty {get; set; }
public CustomEnum[] FailingProperty {get; set; }
}
Is there some strategy in order to implement some generic type converter from string to any custom enum avoiding the explicit registration for each custom enum ? maybe some factory or something in order to create the generic converter inspecting the property target type?
You could try following in a console app:
StringToEnumList.cs
program.cs
Test result
(Note that this converter only work for CustomEnum. For pure Enum ("OkProperty"),you could use your converter.)