【问题标题】:Mapping different object with AutoMapper使用 AutoMapper 映射不同的对象
【发布时间】:2014-03-06 20:24:33
【问题描述】:

我有 UserType 对象,其中名称属性值是类属性的 UserId、LoginName、Email 等。我需要将 UserType 对象映射到 User 对象并从 User 反向映射到 UserType 。我使用了 AutoMapper 库,但我无法实现映射。

public class UserType
{
    public string UserName { get; set; }
    public Attribute[] Attribute { get; set; }
}

用户属性类:

public class Attribute
{
    public string Name { get; set; }
    public string[] Value { get; set; }
}

属性示例:属性attribute = new Attribute { Name = "LoginName", Value = new []{"LoginName"} }

我的用户类:

public class User
{       
    public long UserId { get; set; }
    public string LoginName { get; set; }
    public string Email { get; set; }
    public string LastName { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }   
}

【问题讨论】:

    标签: c# mapping


    【解决方案1】:

    使用反射根据 UserType 中的 Attribute.Name 查找 User 类中的公共属性。使用 PropertyInfo 上的 SetValue 将值从 UserType 设置为 User。

    如果您能够以更自然的方式使用 Automapper 进行操作,请告诉我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-20
      • 1970-01-01
      • 1970-01-01
      • 2017-04-07
      相关资源
      最近更新 更多