private static string GetFieldName<T>(Expression<Func<T, object>> selector) { var visitor = new MemberAccessVisitor(); string propName = visitor.GetMemberName(selector); var fieldName = FieldMapper.TranslateToFieldName(propName); var type = typeof(T); var property = type.GetProperty(propName); var attrs = property.GetCustomAttributes(typeof(FieldAttribute), false); if (attrs.Length != 0) { var attr = (FieldAttribute)attrs[0]; if (attr.Name != null) fieldName = attr.Name; } if (CommonHelper.IsPropertyNotMapped(property)) { // allow to use FieldRef<Document>(d => d.Name) if (propName != "Name") { throw new SharepointCommonException("Cannot use FieldRef<> with property marked as 'NotField'"); } } return fieldName; }
private bool ContainsFieldImpl(PropertyInfo prop) { var propName = prop.Name; var fieldAttrs = prop.GetCustomAttributes(typeof(FieldAttribute), true); if (fieldAttrs.Length != 0) { var spPropName = ((FieldAttribute)fieldAttrs[0]).Name; if (spPropName != null) { propName = spPropName; } } else { propName = FieldMapper.TranslateToFieldName(propName); } return(List.Fields.ContainsFieldWithStaticName(propName)); }
public void Intercept(IInvocation invocation) { var propName = invocation.Method.Name.Substring(4); var prop = invocation.TargetType.GetProperty(propName); var fieldAttrs = prop.GetCustomAttributes(typeof(FieldAttribute), true); string spPropName; if (fieldAttrs.Length != 0) { spPropName = ((FieldAttribute)fieldAttrs[0]).Name ?? propName; } else { spPropName = FieldMapper.TranslateToFieldName(propName); } propName = spPropName; if (invocation.Method.Name.StartsWith("set_")) { throw new SharepointCommonException("Set operations on AfterProperties mapped item not implemented yet!"); } if (!invocation.Method.Name.StartsWith("get_")) { Assert.Inconsistent(); } switch (invocation.Method.Name) { case "get_ParentWeb": invocation.ReturnValue = new QueryWeb(_list.ParentWeb); return; case "get_ParentList": var qWeb = new QueryWeb(_list.ParentWeb); invocation.ReturnValue = qWeb.GetById <Item>(_list.ID); return; case "get_ConcreteParentList": var qWeb1 = new QueryWeb(_list.ParentWeb); invocation.ReturnValue = CommonHelper.MakeParentList(invocation.TargetType, qWeb1, _list.ID); return; case "get_ListItem": { invocation.ReturnValue = null; return; } case "get_Folder": { invocation.ReturnValue = null; return; } } if (CommonHelper.IsPropertyNotMapped(prop)) { // skip props with [NotField] attribute invocation.Proceed(); return; } if (_afterProperties.ContainsKey(propName)) { var field = _list.Fields.GetFieldByInternalName(propName); var val = EntityMapper.ToEntityField(prop, null, field: field, value: _afterProperties[propName], reloadLookupItem: true); invocation.ReturnValue = val; return; } else if (_afterProperties.ContainsKey("vti_" + propName.ToLower())) { var field = _list.Fields.GetFieldByInternalName(propName); var val = EntityMapper.ToEntityField(prop, null, field: field, value: _afterProperties["vti_" + propName.ToLower()], reloadLookupItem: true); invocation.ReturnValue = val; return; } else { var defaultValue = CommonHelper.GetDefaultValue(invocation.Method.ReturnType); invocation.ReturnValue = defaultValue; return; } }