/// <summary> /// if value is PropertyItem and parameter is NumberRangeType /// return the values by NumberRangeType /// </summary> /// <param name="value"></param> /// <param name="targetType"></param> /// <param name="parameter"></param> /// <param name="culture"></param> /// <returns></returns> public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { PropertyItem propertyItem = value as PropertyItem; if (propertyItem != null && parameter is NumberRangeType) { NumberRangeType rangeType = (NumberRangeType)parameter; //just "NumberRange" string metadataName = nameof(NumberRangeAttribute).Replace(nameof(Attribute), string.Empty); NumberRangeAttribute rangeAttribute = propertyItem.Metadata[metadataName] as NumberRangeAttribute; if (rangeAttribute != null) { switch (rangeType) { case NumberRangeType.Minimum: return(rangeAttribute.Minimum); case NumberRangeType.Maximum: return(rangeAttribute.Maximum); case NumberRangeType.Tick: return(rangeAttribute.Tick); case NumberRangeType.Precision: return(rangeAttribute.Precision); } } } return(value); }
public void ShouldAssignAllProperties() { NumberRangeAttribute attribute = new NumberRangeAttribute(10, 20, 30, 40); Assert.AreEqual <double>(10, attribute.Minimum); Assert.AreEqual <double>(20, attribute.Maximum); Assert.AreEqual <double>(30, attribute.Tick); Assert.AreEqual <double>(40, attribute.Precision); }
/// <summary> /// 构造函数 /// </summary> /// <param name="property"></param> public Column(PropertyInfo property, DataBaseType dbType) { if (property == null) { NotMapped = true; return; } Type type = property.PropertyType; if (type.IsClass && !type.FullName.StartsWith("System.", StringComparison.OrdinalIgnoreCase)) { if (property.GetAccessors()[0].IsVirtual) { //外键 IsForeginKey = true; } else { NotMapped = true; return; } } Property = property; object[] attrs = property.GetCustomAttributes(false); if (attrs.Length > 0) { foreach (object attr in attrs) { if (attr is System.ComponentModel.DataAnnotations.Schema.NotMappedAttribute) { NotMapped = true; break; } else if (attr is System.ComponentModel.DataAnnotations.KeyAttribute) { PrimaryKey = true; NotNull = true; } else if (attr is System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedAttribute) { System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedAttribute generated = (System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedAttribute)attr; if (generated.DatabaseGeneratedOption == System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity) { AutoID = true; } } else if (attr is System.ComponentModel.DataAnnotations.RequiredAttribute) { NotNull = true; } else if (attr is System.ComponentModel.DataAnnotations.StringLengthAttribute) { System.ComponentModel.DataAnnotations.StringLengthAttribute length = (System.ComponentModel.DataAnnotations.StringLengthAttribute)attr; StringLenth = length.MaximumLength; } else if (attr is NumberRangeAttribute) { NumberRangeAttribute numberRange = (NumberRangeAttribute)attr; IntergerLength = numberRange.Interger; PointLength = numberRange.Point; } else if (attr is System.ComponentModel.DataAnnotations.Schema.ForeignKeyAttribute) { //指定字段外键 dynamic foreignKey = (System.ComponentModel.DataAnnotations.Schema.ForeignKeyAttribute)attr; if (IsForeginKey) { Column fkCol = Tool.GetForeginKeyColmn(type, dbType, foreignKey.Name); if (fkCol != null) { if (!string.IsNullOrWhiteSpace(foreignKey.Name)) { Column c = Tool.GetForeginKeyColmn(property.DeclaringType, dbType, foreignKey.Name); if (c != null) { Property = c.Property; } ColName = Tool.GetDbColName(foreignKey.Name, dbType); ColType = fkCol.ColType; FK = $"references {type.Name}({fkCol.ColName})"; } else { Property = fkCol.Property; ColName = $"{type.Name}_{fkCol.Name}"; ColType = fkCol.ColType; FK = $"references {type.Name}({fkCol.ColName})"; } } else { NotMapped = true; } } return; } else if (attr is System.ComponentModel.DataAnnotations.Schema.ColumnAttribute) { customColumn = (System.ComponentModel.DataAnnotations.Schema.ColumnAttribute)attr; } } } if (IsForeginKey && string.IsNullOrWhiteSpace(FK)) { //匹配字段外键 Column fkCol = Tool.GetForeginKeyColmn(type, dbType, null); if (fkCol != null) { Property = fkCol.Property; ColName = $"{type.Name}_{fkCol.Name}"; ColType = fkCol.ColType; FK = $"references {type.Name}({fkCol.ColName})"; } else { NotMapped = true; } return; } if (NotMapped) { return; } ColName = null; if (!string.IsNullOrEmpty(customColumn?.Name)) { ColName = Tool.GetDbColName(customColumn.Name, dbType); } else { ColName = Tool.GetDbColName(property.Name, dbType); } if (!string.IsNullOrEmpty(customColumn?.TypeName)) { ColType = customColumn.TypeName; return; } if (type == typeof(string)) { if (dbType == DataBaseType.Sqlite) { if (StringLenth != -1) { ColType = $"NVARCHAR({StringLenth})"; } else { ColType = "TEXT"; } } } else if (type == typeof(int) || type == typeof(uint)) { if (dbType == DataBaseType.Sqlite) { if (AutoID) { ColType = "INTEGER"; } else { ColType = "INT"; } ColType += GetNumberLength(); } } else if (type == typeof(long)) { if (dbType == DataBaseType.Sqlite) { ColType = "INTEGER"; ColType += GetNumberLength(); } } else if (type == typeof(DateTime)) { if (dbType == DataBaseType.Sqlite) { ColType = "DATETIME"; } } else if (type == typeof(double)) { if (dbType == DataBaseType.Sqlite) { ColType = "DOUBLE"; ColType += GetNumberLength(); } } else if (type == typeof(float)) { if (dbType == DataBaseType.Sqlite) { ColType = "FLOAT"; ColType += GetNumberLength(); } } else if (type == typeof(byte)) { if (dbType == DataBaseType.Sqlite) { ColType = "TINYINT"; ColType += GetNumberLength(); } } else if (type == typeof(short) || type == typeof(ushort)) { if (dbType == DataBaseType.Sqlite) { ColType = "SMALLINT"; ColType += GetNumberLength(); } } else if (type == typeof(bool)) { if (dbType == DataBaseType.Sqlite) { ColType = "BOOLEAN"; } } if (!type.IsValueType || (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>))) { NotNull = false; } else { NotNull = true; } }
public void ShouldAssignPrecisionByDefault() { NumberRangeAttribute attribute = new NumberRangeAttribute(10, 20, 30); Assert.AreEqual <double>(0, attribute.Precision); }