Search code examples
c#sitecoreglass-mapper

How can I get the sitecore field from an object property, mapped by glassmapper?


We are using Glass Mapper with Sitecore, with our models we can get the values of sitecore fields. But I want to easily get the sitecore fields(sitecore field type) by using the model without hardcoding any strings (when using GetProperty(), you need the property name string ) into the method.

So I wrote this thing to achieve this, however I am not happy with 2 types need to be passed in when using it since it looks awful when you have a long model identifier.

   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;
    }

The most ideal way is be able to get it like this Model.SomeProperty.SitecoreField(). However I can't figure out how to do the refection from there. Because that can will be a extension for any type.

Thanks!


Solution

  • 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;
    }
    

    Note that I put inherit=true on the GetCustomAttributes method call.
    Otherwise inherited attributes are ignored.