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


This maybe deserves a better answer. So let me explain. In Visual Studio Code you have to set up your startup projects all in the launch.json and the tasks.json files.

Here is a small detailed introduction:

  1. Choose a root project folder (i.e.: D:/anyfolder/myrootfolder)

  2. Create two folders for two projects in the root folder
    2.1 D:/anyfolder/myrootfolder/project1
    2.2 D:/anyfolder/myrootfolder/project2

  3. Open cmd and create two Console-Applications (I use .netcore 2.0)
    3.1 go to folders project1 and project2 using cmd (Command: cd -foldername-)
    3.2 for each of that folders execute the command: dotnet new console

  4. Open the root project folder with Visual Studio Code

  5. Add the following launch.json and tasks.json to the .vscode folder (usually the .vscode folder is generated after clicked the debug-button in VS Code)
    For More information visit:https://code.visualstudio.com/docs/editor/debugging

Sample launch.json file:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": ".NET Core Launch Project1",
            "type": "coreclr",
            "request": "launch",
            "preLaunchTask": "build",
            "program": "${workspaceRoot}/project1/bin/Debug/netcoreapp2.0/project1.dll",
            "args": [],
            "cwd": "${workspaceRoot}/project1",
            "stopAtEntry": false,
            "console": "internalConsole"
        },
        {
            "name": ".NET Core Launch Project2",
            "type": "coreclr",
            "request": "launch",
            "preLaunchTask": "build",
            "program": "${workspaceRoot}/project2/bin/Debug/netcoreapp2.0/project2.dll",
            "args": [],
            "cwd": "${workspaceRoot}/project2",
            "stopAtEntry": false,
            "console": "internalConsole"
        }
    ]
}

Sample tasks.json file:

{
    "version": "0.1.0",
    "command": "dotnet",
    "isShellCommand": true,
    "args": [],
    "tasks": [
        {
            "taskName": "build",
            "args": [
                "${workspaceRoot}/project1/project1.csproj"
            ],
            "isBuildCommand": true,
            "problemMatcher": "$msCompile"
        },
        {
            "taskName": "build",
            "args": [
                "${workspaceRoot}/project2/project2.csproj"
            ],
            "isBuildCommand": true,
            "problemMatcher": "$msCompile"
        }
    ]
}

Don't forget that I used .netcore 2.0. If you use another Target Framework you have to customize the upper sample files of course.

After all you should now see two items in right of the Play(Debug-)button:
.NET Core Launch Project1 and
.NET Core Launch Project2

This worked for me and my purposes...

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 に答える