简体   繁体   中英

C# AutoMapper Generic Mapping Class

I'm trying to do generic mapper class

Mapper.Map<Source, Destination>(source); I am getting error in

Error Message is (an object reference is required for the non-static field method or property)

  public class AutoMapperHelper<TSource, TDestination> where TDestination : class where TSource : class
{
    public TDestination Map(TSource entity)
    {
        var source = new Source<TSource>()
        {
            Value = entity
        };
        var dest = Mapper.Map<Source<TSource>, Destination<TDestination>>(source);
        return dest.Value;

    }
}

public class Source<T>
{
    public T Value { get; set; }
}
public class Destination<T>
{
    public T Value { get; set; }
}

The error seems null reference for the non-static field.

Try below

  public class Source<T> where T:class
{
    public T Value { get; set; }
}
public class Destination<T> where T:class
{
    public T Value { get; set; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM