繁体   English   中英

C# AutoMapper 通用映射类

[英]C# AutoMapper Generic Mapping Class

我正在尝试做通用映射器类

Mapper.Map<来源,目的地>(来源); 我收到错误

错误消息是(非静态字段方法或属性需要对象引用)

  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; }
}

对于非静态字段,该错误似乎为空引用。

试试下面

  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