2017-10-12 104 views
1

我有一點使用AutoMapper一些配置元素的問題。 我有以下類別:使用AutoMapper到映射列表<object>到ConfigurationElementCollection中

public class SocialLinkSettingConfiguration : ConfigurationSection 
{ 
    [ConfigurationProperty("name", IsRequired = true, IsKey = true)] 
    public string Name 
    { 
     get { return this["name"] as string; } 
     set { this["name"] = value; } 
    } 
    [ConfigurationProperty("description", IsRequired = true)] 
    public string Description 
    { 
     get { return this["description"] as string; } 
     set { this["description"] = value; } 
    } 
    [ConfigurationProperty("url", IsRequired = true)] 
    public string Url 
    { 
     get { return this["url"] as string; } 
     set { this["url"] = value; } 
    } 
    [ConfigurationProperty("fontAwesomeClass", IsRequired = false)] 
    public string FontAwesomeClass 
    { 
     get { return this["fontAwesomeClass"] as string; } 
     set { this["fontAwesomeClass"] = value; } 
    } 
    [ConfigurationProperty("imageUrl", IsRequired = false)] 
    public string ImageUrl 
    { 
     get { return this["imageUrl"] as string; } 
     set { this["imageUrl"] = value; } 
    } 
} 

public class SocialLinkSettingConfigurationCollection : ConfigurationElementCollection 
{ 
    public SocialLinkSettingConfiguration this[int index] 
    { 
     get { return (SocialLinkSettingConfiguration)BaseGet(index); } 
     set 
     { 
      if (BaseGet(index) != null) 
      { 
       BaseRemoveAt(index); 
      } 
      BaseAdd(index, value); 
     } 
    } 
    public void Add(SocialLinkSettingConfiguration serviceConfig) 
    { 
     BaseAdd(serviceConfig); 
    } 

    public void Clear() 
    { 
     BaseClear(); 
    } 

    protected override ConfigurationElement CreateNewElement() 
    { 
     return new SocialLinkSettingConfiguration(); 
    } 

    protected override object GetElementKey(ConfigurationElement element) 
    { 
     return ((SocialLinkSettingConfiguration)element).Name; 
    } 

    public void Remove(SocialLinkSettingConfiguration serviceConfig) 
    { 
     BaseRemove(serviceConfig.Name); 
    } 

    public void RemoveAt(int index) 
    { 
     BaseRemoveAt(index); 
    } 

    public void Remove(string name) 
    { 
     BaseRemove(name); 
    } 
} 
public class SocialLinkSetting 
{ 
    public string Name { get; set; } 
    public string Description { get; set; } 
    public string Url { get; set; } 
    public string FontAwesomeClass { get; set; } 
    public string ImageUrl { get; set; } 
} 

我可以創建SocialLinkSetting和SocialLinkSettingConfiguration就好之間的映射。 但是,當我試圖將SocialLinkSetting的列表轉換爲SocialLinkSettingConfigurationCollection時,我每次都失敗。

我想要做的是獲取社交鏈接的列表,並將其轉換爲一個SocialLinkSettingConfigurationCollection對象,該對象中包含所有SocialLinkSetting並轉換爲SocialLinkSettingConfiguration。

我似乎無法做到這一點。每次,它都不知道如何將List轉換爲SocialLinkSettingConfigurationCollection,然後添加每個項目。

任何幫助將不勝感激。

感謝, 本

回答

0

你可以做這樣的事情:

Mapper.Initialize(cfg => 
     { 
      cfg.CreateMap<SocialLinkSetting, SocialLinkSettingConfiguration>(); 
      cfg.CreateMap<List<SocialLinkSetting>, SocialLinkSettingConfigurationCollection>() 
      .AfterMap((source, dest, resolutionContext) => 
       { 
        dest.Clear(); 
        source.ForEach(i => dest.Add(resolutionContext.Mapper.Map<SocialLinkSettingConfiguration>(i))); 
       }); 
     }); 
+0

太謝謝你了。我只需要升級我的Automapper版本就可以實現這個功能(我在4.2版本),而且它做得很好。謝謝! –