1
Vote

How to use ConvertGeneric()?

description

Hi,
I am learning
http://emitmapper.codeplex.com/wikipage?title=Customization%20using%20default%20configurator&referringTitle=Customization%20using%20default%20configurator&ANCHOR#Custom_converters_for_generics

Based on the codes, written in the above website, I wrote the following codes. But each time I run the following codes, I always get an exception. How should I revise the following codes so that I can run it successfully?
Looking forward to your soon reply. Thank you in advance.
public class HashSetToListConverter<T>
        {
            public List<T> Convert(HashSet<T> from, object state)
            {
                if (from == null)
                {
                    return null;
                }
                return from.ToList();
            }
        }
public class Source
        {
            public HashSet<decimal> List { get; set; }

            public Source()
            {
                List = new HashSet<decimal>();
                List.Add(2M);
                List.Add(1M);
                List.Add(8M);
            }
        }

public class Destination1_2
        {
            public List<decimal> List { get; set; }
        }
public void Test4_ConvertGeneric_5_22()
        {
            ObjectsMapper<Source, Destination1_2> mapper = ObjectMapperManager
                .DefaultInstance
                 .GetMapper<Source, Destination1_2>(
                    new DefaultMapConfig()
                    .ConvertGeneric(
                        typeof(HashSet<>),
                        typeof(List<>),
                        new DefaultCustomConverterProvider(typeof(HashSetToListConverter<>))
                    )
                );

            Source source = new Source();
            Destination1_2 destination = null;
            destination = mapper.Map(source);
            Assert.IsNotNull(destination);
            Assert.IsNotNull(destination.List);
            Assert.AreEqual(3, destination.List.Count);
            Assert.AreEqual(source.List.ElementAt(0), destination.List[0]);
            Assert.AreEqual(source.List.ElementAt(1), destination.List[1]);
            Assert.AreEqual(source.List.ElementAt(2), destination.List[2]);
        }

comments