Question

Here is my custom Type Converter.

public class StringListTypeConverter : TypeConverter<String, IEnumerable<String>>
{
    protected override IEnumerable<string> ConvertCore(String source)
    {
        if (source == null)
            yield break;

        foreach (var item in source.Split(','))
            yield return item.Trim();
    }
}

public class Source
{
    public String Some {get;set;}
}


public class Dest
{
    public IEnumerable<String> Some {get;set;}
}

// ... configuration
Mapper.CreateMap<String, IEnumerable<String>>().ConvertUsing<StringListTypeConverter>();
Mapper.CreateMap<Source, Dest>();

The problem: StringListTypeConverter is not being called at all. Dest.Some == null.

Update: Automapper version 1.0.0.155

Was it helpful?

Solution 2

It is verified that everything works fine for Automapper 1.1 RTW

OTHER TIPS

I don't know if this helps or not but I just wrote a similar converter, see below. I don't mind admitting that the yield statements in your converter have me a little confused. :)

public class CsvToStringArrayConverter: ITypeConverter<string, string[]>
{
    #region Implementation of ITypeConverter<string,string[]>

    public string[] Convert(ResolutionContext context)
    {
        if (context.SourceValue != null && !(context.SourceValue is string))
        {
            throw new AutoMapperMappingException(context, string.Format("Value supplied is of type {0} but expected {1}.\nChange the type converter source type, or redirect the source value supplied to the value resolver using FromMember.", 
                                                                        typeof(string), context.SourceValue.GetType()));
        }

        var list = new List<string>();
        var value = (string) context.SourceValue;

        if(!string.IsNullOrEmpty(value))
            list.AddRange(value.Split(','));

        return list.ToArray();
    }

    #endregion
}

I hope it helps, apologies if I've completely misunderstood your problem!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top