我们将 Glass Mapper 与 Sitecore 一起使用,通过我们的模型,我们可以获得 sitecore 字段的值。但我想通过使用模型轻松获取站点核心字段(站点核心字段类型),而不将任何字符串(使用时GetProperty()
,您需要属性名称字符串)硬编码到方法中。
所以我写了这个东西来实现这一点,但是我对使用它时需要传入 2 种类型不满意,因为当你有一个长模型标识符时它看起来很糟糕。
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;
}
最理想的方式就是能这样搞定Model.SomeProperty.SitecoreField()
。但是我不知道如何从那里进行反射。因为这可以是任何类型的扩展。
谢谢!