使用AutoMapper将动态类型(DynamicJsonObject)转换为具体类型对象

时间:2014-10-10 17:29:42

标签: c# asp.net-mvc casting automapper

在CMS中,我试图加载" Widgets"通过类型变量并使用AutoMapper创建要在局部视图中使用的ViewModel实例。

var data = Json.Decode(jsonData);

            for (int i = 0; i < data.Length; i++)
            {
                var d = data[i];

                CmsWgtManager.WidgetType type = Enum.Parse(typeof(CmsWgtManager.WidgetType), d.WidgetType);
                Type modelType = type.GetModelTypeValue();

                var Wgt = Activator.CreateInstance(modelType);

                //these are created externally, just included them here for explanation purposes
                Mapper.CreateMap<dynamic, CmsWgtContentVM>()
                    .ForMember(x => x.Header, o => o.ResolveUsing(s => s.Header));

                Mapper.CreateMap<dynamic, CmsWgtTileStripVM>()
                    .ForMember(x => x.Name, o => o.ResolveUsing(s => s.Name));

                Mapper.CreateMap<dynamic, CmsWgtTileVM>()
                    .ForMember(x => x.Header, o => o.ResolveUsing(s => s.Header));                    


                var widget = new object();
                widget = Mapper.Map(d, Wgt);

                //if (i == 0)
                //{
                //    widget = Mapper.Map<dynamic, CmsWgtContentVM>(d, Wgt as CmsWgtContentVM);
                //}
                //if (i > 0 && i < 3)
                //{
                //    widget = Mapper.Map<dynamic, CmsWgtTileStripVM>(d, Wgt as CmsWgtTileStripVM);
                //}

                widgets.Add(widget as IWgtVM);
            }

这将适用于第一次迭代。在第二个,类型更改,我收到一个投射错误 - &gt; Unable to cast object of type 'Domain.ViewModels.CmsComponents.CmsWgtTileStripVM' to type 'Domain.ViewModels.CmsComponents.CmsWgtContentVM'.

部分Json(前两项)如下。

   {  
  "WidgetType":"Content",
  "Header":"Community Development",
  "Content":"<p><strong>The objective of Place is one and the same: to create a healthy community.</strong> Having a healthy community directly affects the ability to supply services needed by both residents and businesses. Place utilizes Community Development grant programs through the and other sources.<br class=\"k-br\"></p><p>Visit our <a href=\"#\">News &amp; Media</a> page on this website for recently announced grant opportunities.<br class=\"k-br\"></p>",
  "Image":"/Files/Images/about%20section/content-community-dev.jpg",
  "Theme":"None",
  "DefaultView":"Components/WgtContent",
  "ModelFields":"[\"Header\",\"Content\",\"Image\",\"Theme\",\"DefaultView\",\"ModelFields\"]"
   },
   {  
      "WidgetType":"TileStripMedium",
      "Name":"Main Sections",
      "Items":[  
         {  
            "Header":"Territory Map",
            "Content":"<p>View Place territory map from our County Engineer.<br class=\"k-br\"></p><p><img alt=\"\" src=\"/Files/images/about%20section%2Fcontent-territory-map.jpg\"><br></p>",
            "TileLink":"",
            "TileLinkTarget":"",
            "Theme":"GoldAlt",
            "ModelFields":"[\"Header\",\"Content\",\"TileLink\",\"TileLinkTarget\",\"Theme\",\"ModelFields\",\"DefaultView\",\"CustomEditor\"]",
            "DefaultView":"",
            "CustomEditor":""
         },
         {  }
      ],
      "DefaultView":"",
      "CustomEditor":"",
      "ModelFields":"[\"Header\",\"Content\",\"TileLink\",\"TileLinkTarget\",\"Theme\",\"ModelFields\",\"DefaultView\",\"CustomEditor\"]"
   }

0 个答案:

没有答案