Home » ASP » ASP.MVC » Converting anonymous object to Dictionnary

Converting anonymous object to Dictionnary

Some Asp.Net MVC Html Helpers use the possibility to add anonymous object as parameter to assign key value. It is the case of HtmlAttributes parameter. If you want to create your own Html Helper or simply having the possibility to use anonymous object, you may stumble into the case that you need to enumerate keys and values.

public string Url(string action, string controller, string area = null, object routeValues = null)
{
    //Code here
}

The code above is an example. You may want to have a method that generate an url from some parameters. The last parameter named “routeValues” is of type object. This one is created to be used for anonymous object.

    Url("action", "controller", "area", new {Id = "123", Name="This is my name"});

The Url method can then loop through all the properties. Something that can help you is to create an anonymous method for object that convert everything into an IDictionary where it will be easy to manipulate keys (property name) and values.

public static class ObjectExtensions
{
    public static IDictionary<string, object> AsDictionary(this object source, BindingFlags bindingAttr = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance)
    {
        return source.GetType().GetProperties(bindingAttr).ToDictionary
        (
            propInfo => propInfo.Name,
            propInfo => propInfo.GetValue(source, null)
        );
    }
}

It uses reflection to get all properties and from these properties to get all values.

Here is a unit test for the AsDictionary extension method.

[TestClass]
public class ObjectExtensionsTest
{
    [TestMethod]
    public void GivenAnObject_WhenThisOneHasMultipleProperties_ThenDictionary()
    {
        // Arrange
        var objectToConvert = new {Id="Test", Name="Test2"};

        // Act
        var dictionary = objectToConvert.AsDictionary();

        // Assert
        Assert.AreEqual(2,dictionary.Keys.Count);
        Assert.AreEqual(objectToConvert.Id,dictionary["Id"]);
        Assert.AreEqual(objectToConvert.Name,dictionary["Name"]);
    }
}

If you like my article, think to buy my annual book, professionally edited by a proofreader. directly from me or on Amazon. I also wrote a TypeScript book called Holistic TypeScript

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.