首页 > 解决方案 > GlassMapper 升级后 SitecoreTypeCreationContext 抛出异常

问题描述

我遇到了异常:

内部异常:对象引用未设置为对象的实例。在 Glass.Mapper.AbstractService.InstantiateObject(AbstractTypeCreationContext abstractTypeCreationContext)

在 InstantiateObject 的创建上下文中,它不为空,它是填充的项目。如果我添加 typeOf(TItem) 那么它会在 TItem cast 时崩溃,如果我评论它会在 instatinate 时崩溃。我试图删除 ConstructorParameter 但即使在这种情况下它也会崩溃升级前的代码:

var creationContext = new SitecoreTypeCreationContext
        {
           SitecoreService = sitecoreContext,
           RequestedType = typeof(TItem),
           ConstructorParameters = new object[0],
           Item = item,
           InferType = false,
           IsLazy = true,
           Parameters = new Dictionary<string, object>()
         };
var result = sitecoreContext.InstantiateObject(creationContext) as TItem;

升级后的代码

var creationContext = new SitecoreTypeCreationContext
       {
           SitecoreService = mvcContext.SitecoreService,
           Item = item,
           Parameters = new Dictionary<string, object>(),
           Options = new GetOptions()
                {
                    //Type = typeof(TItem), 
                    InferType = false,
                    Lazy = LazyLoading.Enabled,
                    ConstructorParameters = new List<ConstructorParameter>()
                }
            };

var result= mvcContext.SitecoreService.InstantiateObject(creationContext) as TItem;

我可能错过了配置或 RequestedType 它不等于 Option.Type?

标签: glass-mapper

解决方案


推荐阅读