C# 如何从glassmapper映射的对象属性中获取sitecore字段?

C# 如何从glassmapper映射的对象属性中获取sitecore字段?,c#,sitecore,glass-mapper,C#,Sitecore,Glass Mapper,我们将Glass Mapper与Sitecore一起使用,通过我们的模型,我们可以获得Sitecore字段的值。但是我想通过使用模型轻松获取sitecore字段(sitecore字段类型),而无需在方法中硬编码任何字符串(使用GetProperty()时,需要属性名称字符串) 所以我写这篇文章就是为了实现这一点,但是我不喜欢在使用它时需要传入两种类型,因为当您有一个长的模型标识符时,它看起来很糟糕 public static string SitecoreFieldName<T, T

我们将Glass Mapper与Sitecore一起使用,通过我们的模型,我们可以获得Sitecore字段的值。但是我想通过使用模型轻松获取sitecore字段(sitecore字段类型),而无需在方法中硬编码任何字符串(使用
GetProperty()
时,需要属性名称字符串)

所以我写这篇文章就是为了实现这一点,但是我不喜欢在使用它时需要传入两种类型,因为当您有一个长的模型标识符时,它看起来很糟糕

   public static string SitecoreFieldName<T, TU>(Expression<Func<TU>> expr)
    {
         var body = ((MemberExpression)expr.Body);
         var attribute = (typeof(T).GetProperty(body.Member.Name).GetCustomAttributes(typeof(SitecoreFieldAttribute), false)[0]) as SitecoreFieldAttribute;
         return attribute.FieldName;
    }
公共静态字符串SitecoreFieldName(表达式expr)
{
变量body=((MemberExpression)expr.body);
var属性=(typeof(T).GetProperty(body.Member.Name).GetCustomAttributes(typeof(SitecoreFieldAttribute),false)[0])作为SitecoreFieldAttribute;
返回attribute.FieldName;
}
最理想的方法是能够像这样获得它。Model.SomeProperty.SitecoreField()。然而,我不知道如何从那里做反射。因为它可以是任何类型的扩展


谢谢

我不明白为什么我的问题被否决了。那么你认为它已经是完美的代码了

在另一位高级开发人员的帮助下,我今天对它进行了改进,这样就不需要再使用两种类型了,而且使用语法也更加清晰:

public static Field GetSitecoreField<T>(T model, Expression<Func<T, object>> expression) where T : ModelBase
    {
        var body = ((MemberExpression)expression.Body);
        var attributes = typeof(T).GetProperty(body.Member.Name).GetCustomAttributes(typeof(SitecoreFieldAttribute), false);
        if (attributes.Any())
        {
            var attribute = attributes[0] as SitecoreFieldAttribute;
            if (attribute != null)
            {
                return model.Item.Fields[attribute.FieldName];
            }
        }
        return null;
    }
公共静态字段GetSitecoreField(T模型,表达式),其中T:ModelBase
{
var body=((MemberExpression)expression.body);
var attributes=typeof(T).GetProperty(body.Member.Name).GetCustomAttributes(typeof(SitecoreFieldAttribute),false;
if(attributes.Any())
{
var attribute=属性[0]作为SitecoreFieldAttribute;
if(属性!=null)
{
返回model.Item.Fields[attribute.FieldName];
}
}
返回null;
}
我可以这样说:

GetSitecoreField(Container.Model<SomeModel>(), x => x.anyField)
GetSitecoreField(Container.Model(),x=>x.anyField)
希望它能帮助那些在Sitecore中使用Glass Mapper并希望从模型属性中获取当前Sitecore字段的人。

公共静态字符串SitecoreFieldName(表达式字段)
public static string SitecoreFieldName<TModel>(Expression<Func<TModel, object>> field)
{
    var body = field.Body as MemberExpression;

    if (body == null)
    {
        return null;
    }

    var attribute = typeof(TModel).GetProperty(body.Member.Name)
        .GetCustomAttributes(typeof(SitecoreFieldAttribute), true)
        .FirstOrDefault() as SitecoreFieldAttribute;

    return attribute != null
        ? attribute.FieldName
        : null;
}
{ var body=字段。body作为MemberExpression; if(body==null) { 返回null; } var attribute=typeof(TModel).GetProperty(body.Member.Name) .GetCustomAttributes(typeof(SitecoreFieldAttribute),true) .FirstOrDefault()作为SitecoreFieldAttribute; 返回属性!=null ?attribute.FieldName :null; }
请注意,我将
inherit=true
放在
GetCustomAttributes
方法调用上。

否则继承的属性将被忽略。

我知道我应该检查空数组。所以忽略这一点。那么你的问题是什么?对我来说,这看起来很简单,很普通。问题是要进一步改进它。在这种情况下,您需要传入2种类型才能使其工作,当您在数据绑定上下文中使用它时,这在aspx页面上看起来不是很好。很抱歉,我还不能向上投票,但谢谢!这正是我要找的。