Hi, I have built some code to render blocks as json, it works fine. but I am curious if there is a better way?.
The current way of doing it requires a hard connection between the API and the block builders. I have added code to do this dynamically, but my dynamically created types dose not show its properties when reflecting, but thy do show up in the debugger.
.GetProperties() revails no properties, dose someone know why .GetProperties() is empty or have an alternetive solution?
[HttpGet]
[Route("block")]
public IHttpActionResult Get(Guid pageId, string types, Guid? channelId = null)
{
............
List<JsonBlock> returnList = new List<JsonBlock>();
var page = _pageService.Get(pageId);
foreach (var blockContainer in page.Blocks)
{
foreach (var blockItem in blockContainer.Items.OfType<BlockItemLink>())
{
var blockModel = blockItem.BlockSystemId.MapTo<BlockModel>();
if(blockModel != null)
{
var blockTemplate = _fieldTemplateService.Get<FieldTemplateBase>(blockModel.FieldTemplateSystemId);
switch (blockTemplate.Id)
{
case "Takeover":
TakeoverBlockViewModel takeover = blockModel.MapTo<TakeoverBlockViewModel>();
returnList.Add(new JsonBlock { Type = blockTemplate.Id, Data = toDynamic(takeover) });
break;
default:
//Try to do this dynamicly without a switch
string typeName = "Litium.Accelerator.ViewModels.Block." + blockTemplate.Id + "BlockViewModel, Litium.Accelerator";
string qTypeName = Type.GetType(typeName).AssemblyQualifiedName.ToString();
Type viewModelType = Type.GetType(qTypeName);
var viewModel = Activator.CreateInstance(viewModelType);
Mapper.Map(blockModel, viewModel, blockModel.GetType(), viewModelType);
returnList.Add(new JsonBlock { Type = blockTemplate.Id, Data = toDynamic(viewModel) });
}
break;
}
}
}
}
return Json(returnList);
}
private dynamic toDynamic<t>(t obj)
{
IDictionary<string, object> expando = new ExpandoObject();
var porioerties = typeof(t).GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance);
foreach (var propertyinfo in porioerties)
{
var currentvalue = propertyinfo.GetValue(obj);
switch (currentvalue)
{
case ImageModel t1:
var imageModel = (currentvalue as ImageModel);
expando.Add(propertyinfo.Name, new JsonMediaOptions { Thumbnail = imageModel.GetUrlToImage(Size.Empty, new Size(400, 400)).Url, Medium = imageModel.GetUrlToImage(Size.Empty, new Size(1000, 1000)).Url, Full = imageModel.GetUrlToImage(Size.Empty, Size.Empty).Url });
break;
default:
expando.Add(propertyinfo.Name, currentvalue);
break;
}
}
return expando as ExpandoObject;
}
Litium version: 7.3