public static FieldDrawer GetObjectDrawer(FieldAttribute attribute) { FieldDrawer objectDrawer = null; Type type = null; if (!ObjectDrawerUtility.ObjectDrawerForType(attribute.GetType(), ref objectDrawer, ref type, attribute.GetHashCode())) { return(null); } if (objectDrawer != null) { return(objectDrawer); } objectDrawer = (Activator.CreateInstance(type) as FieldDrawer); objectDrawer.Attribute = attribute; ObjectDrawerUtility.objectDrawerMap.Add(attribute.GetHashCode(), objectDrawer); return(objectDrawer); }
private static object GetFieldValue(FieldAttribute fieldAttr, Type valueType, object value) { if (value != null) { if (fieldAttr.DataType == FieldType.Choice || fieldAttr.DataType == FieldType.MultiChoice) { value = EnumExtensions.ParseChoiceValue(valueType, value.ToString()); } else if (fieldAttr.DataType == FieldType.Lookup && (typeof(LookupFieldAttribute).IsAssignableFrom(fieldAttr.GetType()) || fieldAttr.GetType().IsSubclassOf(typeof(LookupFieldAttribute)))) { var lookupFieldMap = fieldAttr as LookupFieldAttribute; if (lookupFieldMap.Result == LookupItemResult.None) { return(value); } if (value is FieldLookupValue) { if (!typeof(FieldLookupValue).IsAssignableFrom(valueType) && !valueType.IsSubclassOf(typeof(FieldLookupValue))) { value = lookupFieldMap.Result == LookupItemResult.Id ? (object)(value as FieldLookupValue).LookupId : (value as FieldLookupValue).LookupValue; if (valueType.IsArray) { var elType = (valueType.GetElementType() ?? valueType.GenericTypeArguments.FirstOrDefault()) ?? typeof(object); value = new[] { SpConverter.ConvertValue(value, elType) }.ToArray(elType); } } } else if (value is FieldLookupValue[]) { if (!lookupFieldMap.IsMultiple) { var lookupValue = (value as FieldLookupValue[]).FirstOrDefault(); if (lookupValue != null) { if (!typeof(FieldLookupValue).IsAssignableFrom(valueType) && !valueType.IsSubclassOf(typeof(FieldLookupValue))) { value = lookupFieldMap.Result == LookupItemResult.Id ? (object)lookupValue.LookupId : lookupValue.LookupValue; } else { value = lookupValue; } } else { value = null; } } else { var elType = (valueType.GetElementType() ?? valueType.GenericTypeArguments.FirstOrDefault()) ?? typeof(object); if (!typeof(FieldLookupValue).IsAssignableFrom(elType) && !elType.IsSubclassOf(typeof(FieldLookupValue))) { var result = lookupFieldMap.Result == LookupItemResult.Id ? (value as FieldLookupValue[]).Select(v => SpConverter.ConvertValue(v.LookupId, elType)) : (value as FieldLookupValue[]).Select(v => SpConverter.ConvertValue(v.LookupValue, elType)); if (valueType.IsArray) { value = result.ToArray(elType); } else { value = result.ToList(elType); } } } } } } return(value); }