在C#中展平或映射对象

时间:2014-12-12 11:30:43

标签: c# c#-4.0

我正在尝试创建将映射或展平对象的泛型类。 让我说我有这2个班级

public class Description
{
    public string Info { get; set; }
}

public class Item
{
    public string Title { get; set; }
    public int WordsCount { get; set; }
    public string Url { get; set; }
    public List<string> Headers { get; set; }
    public User Owner { get; set; }
    public List<Description> Descriptions { get; set; }
}

我尝试创建这样的东西:

Item item = new Item()
    {
        Url = "http",
        Title = "home page",
        Details = new Description() { Info = "some info" },
        WordsCount = 220,
        Headers = new List<string>() { "One", "Two", "Three" },
        Descriptions = new List<Descriptions>() 
            {
                new Description() { Info = "info 1" },
                new Description() { Info = "info 2" },
                new Description() { Info = "info 3" },
            }
    };

在展平/映射之后,我希望有类似的东西:

Item {
   Url: 'http',
   Title: 'home page',
   Details.Info: 'some info',
   WordsCount: '220',
   Headers: 'One^Two^Three',
   Descriptions.0.Info: 'info 1'.
   Descriptions.1.Info: 'info 2',
   Descriptions.2.Info: 'info 3',
 }

我尝试使用AutoMapper,但我想我无法实现这一点 有什么建议吗?

0 个答案:

没有答案