Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从Glass.Mapper.Sitecore升级到Glass.Mapper.Sc时缺少InstanceContext_C#_Sitecore_Glass Mapper - Fatal编程技术网

C# 从Glass.Mapper.Sitecore升级到Glass.Mapper.Sc时缺少InstanceContext

C# 从Glass.Mapper.Sitecore升级到Glass.Mapper.Sc时缺少InstanceContext,c#,sitecore,glass-mapper,C#,Sitecore,Glass Mapper,我正在将一个项目从Glass Mapper v2(Glass.Mapper.Sitecore)升级到v4(Glass.Mapper.Sc),我遇到了一个问题,我们的解决方案使用InstanceContext对象按模板Id获取类。我在新的Glass Mapper中找不到InstanceObject或类似的类。 下面是使用它的代码片段,希望它能显示出我的错误所在 protected InstanceContext InstanceContext; public MappingService(ISi

我正在将一个项目从Glass Mapper v2(Glass.Mapper.Sitecore)升级到v4(Glass.Mapper.Sc),我遇到了一个问题,我们的解决方案使用InstanceContext对象按模板Id获取类。我在新的Glass Mapper中找不到InstanceObject或类似的类。 下面是使用它的代码片段,希望它能显示出我的错误所在

protected InstanceContext InstanceContext;

public MappingService(ISitecoreContext context)
{
    InstanceContext = context.InstanceContext;
}

public T Map<T>(ISitecoreItem sourceItem) where T : class
{
    Type sourceType = InstanceContext.GetClassById(sourceItem.TemplateId);
    if (sourceType != null)
    {
        return Map(sourceItem, sourceType, typeof(T)) as T; 
    }

    return Mapper.Map<T>(sourceItem);
}

// The GetClassById is implemented in an extensions class like this:
public static Type GetClassById(this InstanceContext context, Guid templateId)
{
    if (context.ClassesById.ContainsKey(templateId))
    {
        SitecoreClassConfig config = context.ClassesById[templateId].FirstOrDefault();
        if (config != null)
        {
            return config.Type;
        }
    }
    return default(Type);
}
受保护的InstanceContext InstanceContext;
公共映射服务(ISitecoreContext上下文)
{
InstanceContext=context.InstanceContext;
}
公共T映射(ISitecoreItem sourceItem),其中T:class
{
类型sourceType=InstanceContext.GetClassById(sourceItem.TemplateId);
if(sourceType!=null)
{
返回映射(sourceItem、sourceType、typeof(T))作为T;
}
返回Mapper.Map(sourceItem);
}
//GetClassById在扩展类中实现,如下所示:
公共静态类型GetClassById(此InstanceContext上下文,Guid templateId)
{
if(context.ClassesById.ContainsKey(templateId))
{
SitecoreClassConfig config=context.ClassesById[templateId].FirstOrDefault();
如果(配置!=null)
{
返回config.Type;
}
}
返回默认值(类型);
}
在“new”glass.mapper中,您可以通过查询glass上下文对象的
TypeConfigurations
属性来获取类

大概是这样的:

var configs = Glass.Mapper.Context.Default.TypeConfigurations.Select(x => x.Value as SitecoreTypeConfiguration);
var types = configs.Where(x => x.TemplateId == templateId);