I'm mapping what will soon be about 30-40 different blocks coming from a CMS by way of JSON which indeed comes with the alias of the block type. Is there a way I can use the alias thats coming through as a string to then directly select the C# type/model in my code?
Currently I'm matching them up like this.
switch (block.Type)
{
case "Heading":
mapped = _umbracoMapper.Map<Heading>(block);
break;
case "Carousel":
mapped = _umbracoMapper.Map<Carousel>(block);
break;
case "Alert":
mapped = _umbracoMapper.Map<Alert>(block);
break;
case "Text":
mapped = _umbracoMapper.Map<Text>(block);
break;
default:
break;
}
I understand that the Type isn't really words/letters but is there any way I can reference type to tidy up this repetition, something like..
mapped = _umbracoMapper.Map<[block.Type]>(block);
I'm not 100% sure the above is possible in C#, but I attempted a few versions of the following but its returning System.Type as opposed to (in this case) Blocks.Models.Heading as the mappers Map method requires it to have the target type to be passed to it so it can decipher from which and to what it needs to map. I cant pass it object, dynamic or System.Type, it needs to be the exact type (Heading, Carousel, Alert, Text etc.)
_umbracoMapper.Map(block, Type.GetType("Blocks.Models." + block.Type + ", Blocks"));
Any help would be appreciated!