private static Func <DataRow, TModel> getMapper <TModel>(DataColumnCollection columns) where TModel : new() { Action <DataRow, TModel> mapping = (row, model) => { }; foreach (var prop in typeof(TModel).GetProperties()) { if (!columns.Contains(prop.Name)) { continue; } mapping += (row, model) => { object value = OrionUtils.ConvertType(row[prop.Name], prop.PropertyType); if (value == null) { return; } prop.SetValue(model, value); }; } return(row => { var model = new TModel(); mapping(row, model); return model; }); }
private TProperty[] getValues <TProperty>(LambdaExpression lambdaExpr) { string name = getPropertyName(lambdaExpr); if (!_source.ContainsKey(name)) { return(new TProperty[] { }); } return(_source[name].Values.Select(x => OrionUtils.ConvertType <TProperty>(x)).ToArray()); }
/// <summary>根據 type 參數轉型,若失敗則拋棄</summary> public static IEnumerable <object> Convert(this IEnumerable source, Type type) { foreach (var value in source) { object result = OrionUtils.ConvertType(value, type); if (result == null) { continue; } yield return(result); } }
/*===========================================================*/ /// <summary>取得欄位數值清單</summary> object[] IWhereParams.GetValues(string name) { if (!_source.ContainsKey(name)) { return(new object[] {}); } Type type = typeof(TParams).GetProperty(name).PropertyType; if (type.IsArray) { type = type.GetElementType(); } else if (type.IsGenericType && typeof(IEnumerable).IsAssignableFrom(type)) { type = type.GenericTypeArguments.First(); } return(_source[name].Values.Select(x => OrionUtils.ConvertType(x, type)).ToArray()); }
private string[] parseExprValues(LambdaExpression expr, Type type) { var valueExpr = findValueExpr(expr.Parameters[0], expr.Body); if (valueExpr == null) { return(null); } object value = Expression.Lambda(valueExpr).Compile().DynamicInvoke(); bool isEnumerable = (value is IEnumerable && !(value is string)); IEnumerable enumerable = isEnumerable ? value as IEnumerable : new object[] { value }; string[] values = enumerable .Cast <object>() .Select(x => OrionUtils.ConvertType(x, type)) .Select(x => OrionUtils.ConvertType <string>(x)) .ToArray(); return(values); }
public void ConvertType_Test(object value, Type type) { object result = OrionUtils.ConvertType(value, type); Assert.NotNull(result); }
/// <summary>以 Key 取得 Dictionary 的 Value,找不到就將 Key 轉成 Value 的 Type 回傳</summary> public static V GetOrKey <K, V>(this Dictionary <K, V> dictionary, K key) { return(dictionary.ContainsKey(key) ? dictionary[key] : OrionUtils.ConvertType <V>(key)); }