Thursday, August 10, 2017

dynamic versus Reflection in modern day C#

public void UpdateWhatever(object dto)
{
   WhateverDto whateverDto;
   try
   {
      whateverDto = (WhateverDto)dto;
   }
   catch (Exception exception)
   {
      whateverDto = new WhateverDto();
      dynamic dynamicDto = (dynamic)dto;
      whateverDto.Name = dynamicDto.Name;
      whateverDto.Age = dynamicDto.Age;
      whateverDto.Race = dynamicDto.Race;

...and so on. If we cannot cast off an object we can just map off an object with dynamic so why care about Reflection anymore at all? The dynamic trick is only going to work in times of confidence. If we are unsure of what lies at the thing to inspect then we need to inspect yet. That is when Reflection realistically comes in.

No comments:

Post a Comment