internal static CargoTableFieldDefinition FromProperty(PropertyInfo property) { var fieldType = MatchFieldType(property.PropertyType, out var isCollection, out var isNullable); var listAttr = property.GetCustomAttribute <CargoListAttribute>(); if (listAttr != null && !isCollection) { throw new InvalidOperationException("Invalid CargoListAttribute usage: Annotated member is not collection."); } return(new CargoTableFieldDefinition(CargoModelUtility.ColumnNameFromProperty(property), fieldType, isCollection ? listAttr?.Delimiter ?? "," : null, !isNullable)); }
internal static CargoTableFieldType MatchFieldType(Type clrType, out bool isCollection, out bool isNullable) { isCollection = false; isNullable = !clrType.IsValueType; { var elementType = CargoModelUtility.GetCollectionElementType(clrType); if (elementType != null) { isCollection = true; clrType = elementType; } } if (clrType.IsConstructedGenericType) { var genDef = clrType.GetGenericTypeDefinition(); if (genDef == typeof(Nullable <>)) { // Nullable<> is value type, but it's nullable. isNullable = true; clrType = clrType.GenericTypeArguments[0]; } } if (clrType == typeof(string)) { return(CargoTableFieldType.String); } if (clrType == typeof(bool)) { return(CargoTableFieldType.Boolean); } if ( clrType == typeof(BigInteger) || clrType == typeof(byte) || clrType == typeof(sbyte) || clrType == typeof(short) || clrType == typeof(ushort) || clrType == typeof(int) || clrType == typeof(uint) || clrType == typeof(long) || clrType == typeof(ulong) ) { return(CargoTableFieldType.Integer); } if (clrType == typeof(decimal) || clrType == typeof(float) || clrType == typeof(double) ) { return(CargoTableFieldType.Integer); } if (clrType == typeof(DateTime) || clrType == typeof(DateTimeOffset)) { return(CargoTableFieldType.Datetime); } throw new NotSupportedException($"Not supported type {clrType}."); }