private IDictionary <string, PreValue> GetPreValuesFromDataType(Type type, PreValueContext context = null) { try { IDictionary <string, PreValue> preValues; var factoryAttr = type.GetCodeFirstAttribute <PreValueFactoryAttribute>(); if (context == null) { context = new PreValueContext(type); } if (factoryAttr != null) { preValues = factoryAttr.GetFactory().GetPreValues(context); } else if (type.IsEnum) { preValues = GetEnumPreValues(type); } else if (type.Implements <IPreValueFactory>()) { preValues = ((IPreValueFactory)Activator.CreateInstance(type)).GetPreValues(context); } else { preValues = type.GetCodeFirstAttributes <PreValueAttribute>().ToDictionary(x => x.Alias, x => x.PreValue); } return(preValues); } catch (Exception ex) { throw new CodeFirstException("Failed to get pre-values for type " + type.FullName, ex); } }
public IDictionary <string, PreValue> GetPreValues(PreValueContext context) { var result = new Dictionary <string, PreValue>(); var propAttrs = context.CurrentMember.GetCodeFirstAttributes <ImageCropAttribute>().Select(x => new ImageCropDefinition() { Alias = x.Alias, Height = x.Height, Width = x.Width }); var typeAttrs = this.GetType().GetCodeFirstAttributes <ImageCropAttribute>().Select(x => new ImageCropDefinition() { Alias = x.Alias, Height = x.Height, Width = x.Width });; var typePropAttrs = GetCropsFromProperties(); var attrs = propAttrs.Union(typePropAttrs.Values).Union(typeAttrs).ToArray(); result.Add("crops", new PreValue(JsonConvert.SerializeObject(attrs))); return(result); }
private IDictionary <string, PreValue> GetPreValuesFromProperty(PropertyInfo instance) { IDictionary <string, PreValue> preValues; var factoryAttr = instance.GetCodeFirstAttribute <InstancePreValueFactoryAttribute>(); var instanceAttributes = instance.GetCodeFirstAttributes <InstancePreValueAttribute>(); var redirect = instance.GetCustomAttributes().FirstOrDefault(x => x.GetType().Implements <IDataTypeRedirect>()) as IDataTypeRedirect; PreValueContext context = new PreValueContext(instance); if (factoryAttr != null) { preValues = factoryAttr.GetFactory().GetPreValues(context); } else if (instanceAttributes.Count() > 0) { try { preValues = instanceAttributes.ToDictionary(x => x.Alias, x => x.PreValue); } catch (ArgumentException ex) { throw new CodeFirstException("Duplicate pre-value alias on data type instance " + instance.DeclaringType.Name + "." + instance.Name, ex); } } else if (redirect != null) { if (redirect is IInitialisablePropertyAttribute) { (redirect as IInitialisablePropertyAttribute).Initialise(instance); } preValues = GetPreValuesFromDataType(redirect.Redirect(instance), context); } else { preValues = GetPreValuesFromDataType(instance.PropertyType, context); } return(preValues); }
public override IDictionary <string, PreValue> GetPreValues(PreValueContext context) { return(base.GetPreValuesInternal(context, 1)); }