private void SetFieldSlots(FieldSetting fieldSetting, ISupportsDynamicFields handler) { if (fieldSetting.DataTypes.Length == 0) { return; } var handlerType = handler.GetType(); Type[][] slots = fieldSetting.HandlerSlots; var dynamicFieldMetadata = handler.GetDynamicFieldMetadata(); for (int i = 0; i < fieldSetting.Bindings.Count; i++) { string propName = fieldSetting.Bindings[i]; PropertyInfo propInfo = null; propInfo = handlerType.GetProperty(propName); Type propertyType = null; bool readOnly = false; if (propInfo != null) { //-- code property propertyType = propInfo.PropertyType; readOnly = readOnly || !ContentTypeManager.PropertyHasPublicSetter(propInfo); } else { //-- generic property propertyType = dynamicFieldMetadata[propName].PropertyType; readOnly = !dynamicFieldMetadata[propName].CanWrite; } for (int j = 0; j < slots[i].Length; j++) { if (slots[i][j].IsAssignableFrom(propertyType)) { fieldSetting.HandlerSlotIndices[i] = j; fieldSetting.PropertyIsReadOnly = readOnly; break; } } } fieldSetting.Initialize(); }
private void SetFieldSlots(Type handlerType) { //-- Field slot indices and readonly. if (handlerType == null) { throw new ContentRegistrationException(String.Concat("Unknown ContentHandler: '", this.HandlerName, "'. ContentType: ", this.Name)); } foreach (FieldSetting fieldSetting in this.FieldSettings) { if (fieldSetting.DataTypes.Length == 0) { continue; } Type[][] slots = fieldSetting.HandlerSlots; for (int i = 0; i < fieldSetting.Bindings.Count; i++) { string propName = fieldSetting.Bindings[i]; PropertyInfo propInfo = null; propInfo = handlerType.GetProperty(propName); Type propertyType = null; bool readOnly = false; if (propInfo != null) { //-- code property propertyType = propInfo.PropertyType; readOnly = readOnly || !ContentTypeManager.PropertyHasPublicSetter(propInfo); } else { //-- generic property if (handlerType != typeof(GenericContent) && !handlerType.IsSubclassOf(typeof(GenericContent))) { throw new ContentRegistrationException(String.Concat("Unknown property: ", propName), this.Name, fieldSetting.Name); } RepositoryDataType dataType = fieldSetting.DataTypes[i]; switch (dataType) { case RepositoryDataType.String: case RepositoryDataType.Text: propertyType = typeof(string); break; case RepositoryDataType.Int: propertyType = typeof(Int32); break; case RepositoryDataType.Currency: propertyType = typeof(decimal); break; case RepositoryDataType.DateTime: propertyType = typeof(DateTime); break; case RepositoryDataType.Binary: propertyType = typeof(BinaryData); break; case RepositoryDataType.Reference: propertyType = typeof(NodeList <Node>); break; default: throw new ContentRegistrationException(String.Concat("Unknown datatype: ", dataType), this.Name, fieldSetting.Name); } } for (int j = 0; j < slots[i].Length; j++) { ////-- for your information: //typeof(Node).IsAssignableFrom(typeof(User)) ==> true //typeof(User).IsAssignableFrom(typeof(Node)) ==> false //-- this is the bad code: //if (propertyType.IsAssignableFrom(slots[i][j])) //-- this is the good: if (slots[i][j].IsAssignableFrom(propertyType)) { fieldSetting.HandlerSlotIndices[i] = j; fieldSetting.PropertyIsReadOnly = readOnly; break; } } } fieldSetting.Initialize(); } }