简体   繁体   English

C# AutoMapper 通用映射类

[英]C# AutoMapper Generic Mapping Class

I'm trying to do generic mapper class我正在尝试做通用映射器类

Mapper.Map<Source, Destination>(source); Mapper.Map<来源,目的地>(来源); 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; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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