samedi 18 mars 2017

Nested dictionary to class by type

I'm trying to convert a nested Dictionary to nested class base on its type. For example. I have this dictionary:

{
    "owner": {
        "name": {
            "first": "Linh",
            "last": "Nguyen"
        },
        "age": "Nguyen"
    },
    "power": {
        "index": "1",
        "range": "5"
    }
}

And I have this nested class :

    public class AccountRegistrationViewModel
    {
        /// <summary>
        /// Account owner.
        /// </summary>
        [Required]
        public Owner Owner { get; set; }

        /// <summary>
        /// User age.
        /// </summary>
        [Required]
        public int Age { get; set; }

        /// <summary>
        /// Account photo.
        /// </summary>
        [Required]
        public List<HttpFile> Photos { get; set; }
    }

    public class Owner
    {
        public Name Name { get; set; }
    }

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

        public string Last { get; set; }
    }

And I have this function:

T Convert<T>(IDictionary<string, object> input, Type classType)
{
    // Do something.
    return T;
}

In javascript, it is very easy to do this task, but currently, I don't know how to achieve this task by using C#.

Can someone help me please?

Thank you,





Aucun commentaire:

Enregistrer un commentaire