public static List <string> Candidates(string name, Type type, string canRemovePrefix = null) { var names = new List <string>(2) { name }; // special handling of xxxId to xxx for primitive types (int, long, etc) if (type.IsPrimitiveOrEnum() || (Types.IsNullable(type) && type.NullableOf().IsPrimitiveOrEnum())) { if (name.EndsWith("Id", StringComparison.OrdinalIgnoreCase)) { var left = name.Substring(0, name.Length - 2); if (!string.IsNullOrWhiteSpace(left) && !names.Contains(left)) { names.Add(left); } } else { names.Add(name + "Id"); } } // see if we need to replace underscores int max = names.Count; for (int i = 0; i < max; i++) { if (names[i].IndexOf('_') >= 0) { var left = names[i].Replace("_", ""); if (!string.IsNullOrWhiteSpace(left) && !names.Contains(left)) { names.Add(left); } } } // remove (optional) prefix, e.g. remove ORDER from ORDER_ID if (!string.IsNullOrEmpty(canRemovePrefix)) { max = names.Count; for (int i = 0; i < max; i++) { if (names[i].StartsWith(canRemovePrefix, StringComparison.OrdinalIgnoreCase)) { var left = names[i].Substring(canRemovePrefix.Length); if (!string.IsNullOrWhiteSpace(left) && !names.Contains(left)) { names.Add(left); } } } } return(names); }
static string SetMethodName(Type colType) { if (Types.IsNullable(colType)) { colType = colType.GetGenericArguments()[0]; } if (colType == typeof(float)) { return("SetFloat"); } return("Set" + colType.Name); }
static MethodCallExpression SetValue(ParameterExpression result, ParameterExpression item, Thing from, Column to) { Expression value = Expression.PropertyOrField(item, from.Name); if (to.Type != from.Type) { // type if not the same, can it be assigned? if (Types.CanBeCast(from.Type, to.Type)) { value = Expression.Convert(value, to.Type); } else if (Types.IsNullable(from.Type) && from.Type.GetGenericArguments()[0] == to.Type) { value = Expression.Convert(value, to.Type); } else if (Types.IsNullable(from.Type) && Types.CanBeCast(from.Type.GetGenericArguments()[0], to.Type)) { value = Expression.Convert(value, to.Type); } else { return(null); } } if (to.Type == typeof(byte[])) { var setBytes = typeof(SqlDataRecord).GetMethod("SetBytes", new[] { typeof(int), typeof(long), typeof(byte[]), typeof(int), typeof(int) }); Contract.Assert(setBytes != null); return(Expression.Call(result, setBytes, Expression.Constant(to.Ordinal), Expression.Constant(0L), value, Expression.Constant(0), Expression.PropertyOrField(value, "Length"))); } var setMethod = typeof(SqlDataRecord).GetMethod(SetMethodName(to.Type), new[] { typeof(int), to.Type }); Contract.Assert(setMethod != null); return(Expression.Call(result, setMethod, Expression.Constant(to.Ordinal), value)); }
static Expression ReadValue(ParameterExpression input, Mapping map) { Expression value = Expression.PropertyOrField(input, map.From.Name); var fromType = map.From.Type; var toType = map.To.Type; if (fromType == toType) { return(value); } if (Types.CanBeCast(fromType, toType)) { return(Expression.Convert(value, toType)); } if (Types.IsNullable(fromType)) { var nullableArgType = Types.NullableOf(fromType); if (nullableArgType == toType) { return(Expression.Call(value, fromType.GetMethod("GetValueOrDefault", Type.EmptyTypes))); } if (Types.IsNullable(toType) && Types.CanBeCast(nullableArgType, toType)) { // nullable<> to nullable<> conversion must handle null to null as a special case return(Expression.Condition( Expression.PropertyOrField(value, "HasValue"), Expression.Convert(Expression.Call(value, fromType.GetMethod("GetValueOrDefault", Type.EmptyTypes)), toType), Expression.Default(toType))); } if (Types.CanBeCast(nullableArgType, toType)) { return(Expression.Convert(Expression.Call(value, fromType.GetMethod("GetValueOrDefault", Type.EmptyTypes)), toType)); } } throw new InvalidOperationException("Should never get here has types compatibility has been checked"); }