2014-03-06 205 views
0

我有UserType对象,其中Name属性值是User属性的UserId,LoginName,Email等等。我需要将UserType对象映射到User对象,并将User映射到UserType。我使用了AutoMapper库,但我无法实现映射。用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; } 
} 

属性例如:属性属性=新属性{名称= “LoginName将”,值=新[] { “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; } 
} 

回答

0

使用反射基于在用户类型Attribute.Name找到用户类的公共财产。使用PropertyInfo上的SetValue将UserType中的值设置为User。

如果您能够以更自然的方式使用Automapper,请让mi知道。