Skip to content

Build status Coveralls branch NuGet

LSL.Dictionaries

Helpers for dictionaries. Currently supports mapping an object to a dictionary and a dictionary into an object.

Assumed class definitions

The following quick start examples assume the following class definitions have been defined:

public class MyObject
{
    public int AValue { get; set; }
    public Inner Inner { get; set; } = new Inner();
}

public class Inner
{
    public string Name { get; set; }
}

Object Extensions

Convert an object to a dictionary using the ToDictionary() object extensions method.

using LSL.Dictionaries.Extensions;
...
var theDictionary = new MyObject()
    {
        AValue = 12,
        Inner = new Inner
        {
            Name = "Als"
        }
    }
    .ToDictionary();

/*
    theDictionary will contain:

    ["AValue"] = 12
    ["Inner"] = new Dictionary<string, object>
    {
        ["Name"] = "Als"
    }
*/

Dictionary Extensions

Convert an IDictionary<string, object> to an object:

using LSL.Dictionaries.Extensions;
...

var theObject = new Dictionary<string, object>
{
    ["AValue"] = 12,
    ["Inner"] = new Dictionary<string, object>
    {
        ["Name"] = "Als"
    }
}.ToObject<MyObject>();

/*
    theObject will be:
    {
        AValue = 12,
        Inner = {
            Name = "Als"
        }
    }
*/