Using reflection to dynamically set the value on the class property in ASP.NET MVC

We have a situation where we received some data from API and we save each file into NameValueCollection. Each property match our class property, so we want to use reflection to dynamically set values for our class from values in NameValueCollection. This is very fast and easy way to not repeat ourselves when mapping values from one object to another.

I used unit test to demonstrate functionality. 
class PolicyManager
    {
        public string ImportJobTypeID { get; set; }
        public string ImportDate { get; set; }
        public string ImportSourceID { get; set; }
        public string APIUserID { get; set; }
        public string APIUserKey { get; set; }
        public string ImportTypeID { get; set; }
        public string ImportUID { get; set; }
        public string Title { get; set; }
        public string FirstName { get; set; }
        public string Surname { get; set; }
        public string Company { get; set; }
        public string Tel { get; set; }
        public string Email { get; set; }
        public string DOB { get; set; }
        public string Address1 { get; set; }
        public string Address2 { get; set; }

        public static NameValueCollection RetrieveDataFromAPIMockUp()
        {
            dynamic values = new NameValueCollection() {
                {"ImportJobTypeID", "1"},
                {"ImportDate", DateTime.Today.ToString()},
                {"ImportSourceID", "1"},
                {"APIUserID", "7"},
                {"APIUserKey", "f4a03091a6a505d10fdebeb707d59e42"},
                {"ImportTypeID", "3"},
                {"ImportUID", DateTime.Now.ToString()},
                {"Title", "Mr"},
                {"FirstName", "Adam"},
                {"Surname", "Black"},
                {"Company", "AbDeveloper.com"},
                {"Tel", "07817 458784"},
                {"Email", "ab@example.com"},
                {"DOB", "10/04/1974"},
                {"Address1", "36 Portland"},
                {"Address2", "Avenue"}
              
            };

            return values;
        }
    }

Because we know exactly what properties we are getting from API it is ok to create a class that matches those properties.
Static method RetrieveDataFromAPIMockUp will generate some dummy data(for test purposes only, this is what we would get from API).

Now simple test.

{
        [TestMethod]
        public void TestReflection()
        {
            PolicyManager pManager = new PolicyManager();

            NameValueCollection values = PolicyManager.RetrieveDataFromAPIMockUp();

            foreach (var value in values)
            {
                PropertyInfo propInfo = typeof(PolicyManager).GetProperty(value.ToString());
                if (propInfo != null)
                {
                    propInfo.SetValue(pManager, values[value.ToString()]);
                }
            }
        }
    }

Pic 1 shows the result and confirm that method works as expected.
If API will change and send more properties our method will still works but will ignore it.

Using reflection to dynamically set the value on the class property in ASP.NET MVC
pic 1

Comments