1

I am having a bit of a problem using AutoMapper with some Configuration Elements. I have the following classes:

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

I can create a mapping between SocialLinkSetting and SocialLinkSettingConfiguration just fine. However, when I am trying to convert a list of SocialLinkSetting to SocialLinkSettingConfigurationCollection, I fail every time.

What I am trying to do is take a list of Social Links, and convert it to a single SocialLinkSettingConfigurationCollection object that has all of the SocialLinkSetting inside and converted to SocialLinkSettingConfiguration.

I can't seem to do this. Each time, it doesn't know how to convert List to the SocialLinkSettingConfigurationCollection, then add each of the items.

Any help would be greatly appreciated.

Thanks, Ben


(one line if) ? bool is true - give me string

I can't wrap by head around this even though I've read many stackoverflow threads.

I check if the page has a specific property value like so:

bool categoryCount = CurrentPage.HasValue("blogCategories");

I want to transform this into a one-line if statement so I can parse a string to a class like illustrated below:

string situation = (categoryCount.ToString() == "true") ? '' : '';

PS: I apologize for the missing logic behind my thoughts/goal. I'm new at programming.

4

1 回答 1

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)));
                });
        });
于 2017-10-12T17:23:32.283 回答