private void MergeDataTypeInstanceWithAncestor(DataTypeInstanceAttribute customDataTypeAttribute, PropertyInfo property, DataTypeRegistration underlyingType) { if (customDataTypeAttribute.Name == null) { customDataTypeAttribute.Name = underlyingType.DataTypeInstanceName + " (" + property.DeclaringType.Name + "." + property.Name + ")"; } if (customDataTypeAttribute.PropertyEditorAlias == null) { customDataTypeAttribute.PropertyEditorAlias = underlyingType.PropertyEditorAlias; } if (customDataTypeAttribute.ConverterType == null) { customDataTypeAttribute.ConverterType = underlyingType.ConverterType; } if (customDataTypeAttribute.DbType == DatabaseType.None) { customDataTypeAttribute.DbType = underlyingType.DbType; } }
private void InferInstanceRegistrationProperties(PropertyInfo instance, Type type, DataTypeInstanceAttribute dataTypeInstanceAttribute) { //Try to get properties from underlying data type if any essential properties are null DataTypeRegistration underlyingType; if (DataTypeRegister.TryGetRegistration(type, out underlyingType)) { MergeDataTypeInstanceWithAncestor(dataTypeInstanceAttribute, instance, underlyingType); } else if (dataTypeInstanceAttribute.Name == null) { throw new CodeFirstException("A [DataTypeInstance] attribute must specify a value for data type name, unless it is applied to a document property whose type is a valid data type (in which case the data type name of the underlying data type is inherited)"); } }
private DataTypeRegistration BuildDataTypeRegistration(PropertyInfo instance, out bool typeExisted) { DataTypeInstanceAttribute dataTypeInstanceAttribute = instance.GetCodeFirstAttribute <DataTypeInstanceAttribute>(); if (dataTypeInstanceAttribute == null && instance.GetCustomAttributes(false).Any(x => x.GetType().Implements <IDataTypeInstance>())) { dataTypeInstanceAttribute = new DataTypeInstanceAttribute(); } var dataType = instance.GetCodeFirstAttribute <ContentPropertyAttribute>().DataType; typeExisted = true; var redirect = instance.GetCustomAttributes().FirstOrDefault(x => x.GetType().Implements <IDataTypeRedirect>()) as IDataTypeRedirect; Type targetType; if (redirect != null) { if (redirect is IInitialisablePropertyAttribute) { (redirect as IInitialisablePropertyAttribute).Initialise(instance); } targetType = redirect.Redirect(instance); } else { targetType = instance.PropertyType; } if (targetType.IsConstructedGenericType) { EnsureGenericTypeRegistration(targetType, ref typeExisted); } if (dataTypeInstanceAttribute == null && string.IsNullOrWhiteSpace(dataType)) //no data type override specified { return(CheckTypeRegistrationForProperty(instance, targetType)); } else if (!string.IsNullOrWhiteSpace(dataType)) //data type override specified in property attribute { typeExisted = true; //never modify any data type when using PEVCs - we only support back-office managed data types with PEVCs return(CreateUmbracoConverterDataType(instance, dataType)); } if (dataTypeInstanceAttribute.HasNullProperties) //data type override specified in instance attribute { InferInstanceRegistrationProperties(instance, targetType, dataTypeInstanceAttribute); } typeExisted = false; DataTypeRegistration dataTypeRegistration = new DataTypeRegistration() { ClrType = targetType, ConverterType = dataTypeInstanceAttribute.ConverterType, DataTypeInstanceName = dataTypeInstanceAttribute.Name, PropertyEditorAlias = dataTypeInstanceAttribute.PropertyEditorAlias, DbType = dataTypeInstanceAttribute.DbType, CodeFirstControlled = true }; _registerController.Register(instance, dataTypeRegistration); return(dataTypeRegistration); }