0

I Need to map a generic key value list to my model class.

Below is the KeyValues class.

public class KeyValues
{
    public string Key   { get; }
    public string Value { get; set; }
}

Getting this as input in IList<KeyValues> with below values

Key = "SystemId"
Value = "12"

Key = "SystemName"
Value = "LTPVBN21"

Key = "Location"
Value = "NJ2"

I want to map this to my below SystemInformation class properties. Based on Key need to set value in corresponding property.

public class SystemInformation
{
    public string SystemId   { get; set; }
    public string SystemName { get; set; }
    public string Location   { get; set; }
}

Right now iam looping the IList<KeyValues> object and setting the values in model comparing the key.

Is there any other way i can achieve this using Automapper or any other option since i have to do this similar operation for multiple models.

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
Das
  • 59
  • 1
  • 9

2 Answers2

0

You need to use reflection here for this and it would be something like following:

SystemInformation information = new SystemInformation();
foreach(var item in values)
{
    information.GetType()
                    .GetProperty(item.Key)
                    .SetValue(information, item.Value);
}

See the working DEMO Fiddle.

I am using specifically SystemInformation object but it could be anything and you could call GetType() to get the type details and then further proceed by calling reflection methods.

Hope it helps.

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
0
using System.Collections.Generic;
using System.Reflection;

namespace ConsoleApp1
{
    class Program
    {

        static void Main(string[] args)
        {
            List<KeyValues> kv = new List<KeyValues>();
            kv.Add(new KeyValues() { Key = "SystemId", Value = "12" });
            kv.Add(new KeyValues() { Key = "SystemName", Value = "LTPVBN21" });
            kv.Add(new KeyValues() { Key = "Location", Value = "NJ2" });

            SystemInformation si = new SystemInformation();

            foreach (KeyValues k in kv)
            {
                PropertyInfo pi = typeof(SystemInformation).GetProperty(k.Key);

                pi.SetValue(si, k.Value);
            }

    }


    public class KeyValues
    {
        public string Key { get; set; }
        public string Value { get; set; }
    }

    public class SystemInformation
    {
        public string SystemId { get; set; }

        public string SystemName { get; set; }

        public string Location { get; set; }
    }
}

}
Aldert
  • 4,209
  • 1
  • 9
  • 23
  • Dont get the dv on this answer. its perfectly valid – Jamiec Aug 10 '18 at 15:09
  • No difference, helping Das out with working example. Copied it and did not see your answer yet – Aldert Aug 10 '18 at 15:14
  • I think you misunderstood. I didnt downvote you (In fact I upvoted to cancel it out), and there is no need for me to answer, this is a perfectly good one - as is the other similar one. – Jamiec Aug 10 '18 at 15:16