private void ParseSubDataField() { PropertyInfo[] propertys = ObjectType.GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo pi in propertys) { IDataFieldConfig config = ConfigManager.LoadDataFieldConfig(pi); if (config != null) { Type type = pi.PropertyType; string name = string.IsNullOrEmpty(config.Name) ? pi.Name : config.Name; string filedName = string.Format("{0}_{1}", Name, name); string indexName = string.Format("{0}_{1}", IndexName, pi.Name); DataFieldMapping mapping = DataFieldMapping.CreateDataFieldMapping(type, pi, filedName, indexName, config, EntityMapping, ObjectType); PrimitiveFieldMapping primitiveFieldMapping = mapping as PrimitiveFieldMapping; if (primitiveFieldMapping != null) { primitiveFieldMapping.IsIdentity = false; primitiveFieldMapping.IsPrimaryKey = false; } mapping.Handler = new PropertyHandler(pi); _fieldMappingDictionary.Add(mapping.IndexName, mapping); if (mapping.Name != mapping.IndexName) { _fieldMappingAlterNameDictionary.Add(mapping.Name, mapping); } } } if (_fieldMappingDictionary.Count == 0) { throw new LightDataException(RE.ComplexFieldHaveNotSubFields); } }
protected void InitialDataFieldMapping() { PropertyInfo[] propertys = ObjectType.GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo pi in propertys) { //字段属性 IDataFieldConfig config = ConfigManager.LoadDataFieldConfig(pi); if (config != null) { Type type = pi.PropertyType; string name = string.IsNullOrEmpty(config.Name) ? pi.Name : config.Name; DataFieldMapping mapping = DataFieldMapping.CreateDataFieldMapping(type, pi, name, pi.Name, config, this, ObjectType); mapping.Handler = new PropertyHandler(pi); _fieldMappingDictionary.Add(mapping.IndexName, mapping); if (mapping.Name != mapping.IndexName) { _fieldMappingAlterNameDictionary.Add(mapping.Name, mapping); } } } if (_fieldMappingDictionary.Count == 0) { throw new LightDataException(RE.DataFieldsIsNotExists); } }
internal static IDataFieldConfig LoadDataFieldConfig(PropertyInfo pi) { IDataFieldConfig config = null; if (config == null) { LightDataConfig lightDataConfig = GetConfig(); if (lightDataConfig != null && lightDataConfig.ContainDataTableConfig(pi.ReflectedType)) { DataTableConfig dtconfig = lightDataConfig.GetDataTableConfig(pi.ReflectedType); IConfiguratorFieldConfig fieldConfig = dtconfig[pi.Name]; if (fieldConfig != null) { if (fieldConfig is IgnoraFieldConfig) { return(null); } config = dtconfig[pi.Name] as DataFieldConfig; } } } if (config == null) { DataFieldAttribute[] attributes = AttributeCore.GetPropertyAttributes <DataFieldAttribute>(pi, true); if (attributes.Length > 0) { config = attributes[0]; } } return(config); }
public static PropertyInfo GetSpecifiedProperty(string specifiedPropertyName, Type mainType) { PropertyInfo[] propertys = mainType.GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo pi in propertys) { if (pi.Name == specifiedPropertyName && pi.PropertyType == typeof(Boolean)) { IDataFieldConfig dfconfig = ConfigManager.LoadDataFieldConfig(pi); if (dfconfig != null) { continue; } IAggregateFieldConfig afconfig = ConfigManager.LoadAggregateFieldConfig(pi); if (afconfig != null) { continue; } return(pi); } } return(null); }
public static DataFieldMapping CreateDataFieldMapping(Type type, PropertyInfo property, string fieldName, string indexName, IDataFieldConfig config, DataEntityMapping mapping, Type mainType) { if (!Regex.IsMatch(fieldName, _fieldRegex, RegexOptions.IgnoreCase)) { throw new LightDataException(RE.FieldNameIsInvalid); } DataFieldMapping fieldMapping = null; bool isNullable = false; string dbType = config.DBType; if (type.IsGenericType) { Type frameType = type.GetGenericTypeDefinition(); if (frameType.FullName == "System.Nullable`1") { Type[] arguments = type.GetGenericArguments(); type = arguments [0]; isNullable = true; } } isNullable = isNullable || config.IsNullable; if (type.IsArray && type.FullName != "System.Byte[]") { throw new LightDataException(RE.TheTypeOfDataFieldIsNotRight); } else if (type.IsGenericParameter | type.IsGenericTypeDefinition) { throw new LightDataException(RE.TheTypeOfDataFieldIsNotRight); } else if (type.IsEnum) { EnumFieldMapping enumFieldMapping = new EnumFieldMapping(type, fieldName, indexName, mapping, isNullable, dbType); // enumFieldMapping.IsNullable = config.IsNullable; if (config.DefaultValue != null) { if (config.DefaultValue is String) { enumFieldMapping.DefaultValue = Enum.Parse(type, config.DefaultValue as String, true); } else { Array arr = Enum.GetValues(type); foreach (object obj in arr) { if (obj.Equals(config.DefaultValue)) { enumFieldMapping.DefaultValue = config.DefaultValue; break; } } } } fieldMapping = enumFieldMapping; } else { TypeCode code = Type.GetTypeCode(type); if (code == TypeCode.DBNull) { throw new LightDataException(RE.TheTypeOfDataFieldIsNotRight); } if (code == TypeCode.Empty) { throw new LightDataException(RE.TheTypeOfDataFieldIsNotRight); } else if (code == TypeCode.Object && type.FullName != "System.Byte[]") { ComplexFieldMapping complexFieldMapping = new ComplexFieldMapping(type, fieldName, indexName, mapping, isNullable); // complexFieldMapping.IsNullable = config.IsNullable; fieldMapping = complexFieldMapping; } else { PrimitiveFieldMapping primitiveFieldMapping = new PrimitiveFieldMapping(type, fieldName, indexName, mapping, isNullable, dbType); primitiveFieldMapping.IsIdentity = config.IsIdentity; primitiveFieldMapping.IsPrimaryKey = config.IsPrimaryKey; // primitiveFieldMapping.IsNullable = config.IsNullable; // primitiveFieldMapping.DBType = config.DBType; if (config.DefaultValue != null) { if (config.DefaultValue.GetType() == type) { primitiveFieldMapping.DefaultValue = config.DefaultValue; } else { primitiveFieldMapping.DefaultValue = Convert.ChangeType(config.DefaultValue, type); } } fieldMapping = primitiveFieldMapping; } } // if (isNullAbleType) { // fieldMapping.IsNullable = true; // } if (fieldMapping.IsNullable) { PropertyInfo specifiedProperty = GetSpecifiedProperty(property.Name + "Specified", mapping.ObjectType); if (specifiedProperty != null) { fieldMapping._specifiedHandler = new PropertyHandler(specifiedProperty); } } if (config.DataOrder > 0) { fieldMapping.DataOrder = config.DataOrder - 1; } return(fieldMapping); }