} // End Function IsSimpleType public virtual System.Collections.Generic.List <T> GetList <T>(System.Data.IDbCommand cmd) { System.Collections.Generic.List <T> lsReturnValue = new System.Collections.Generic.List <T>(); T tThisValue = default(T); System.Type tThisType = typeof(T); using (System.Data.IDataReader idr = ExecuteReader(cmd)) { if (IsSimpleType(tThisType)) { while (idr.Read()) { object objVal = idr.GetValue(0); // tThisValue = System.Convert.ChangeType(objVal, T), tThisValue = (T)ConvertResult <T>(objVal, true); lsReturnValue.Add(tThisValue); } // End while (idr.Read()) } else { int iFieldCount = idr.FieldCount; Setter_t <T>[] mems = new Setter_t <T> [iFieldCount]; for (int i = 0; i < iFieldCount; ++i) { string strName = idr.GetName(i); mems[i] = LinqHelper.GetSetter <T>(strName); } // Next i while (idr.Read()) { tThisValue = System.Activator.CreateInstance <T>(); for (int i = 0; i < iFieldCount; ++i) { Setter_t <T> setter = mems[i]; if (setter != null) { object objVal = idr.GetValue(i); setter(tThisValue, objVal); } } // Next i lsReturnValue.Add(tThisValue); } // Whend } // End if IsSimpleType(tThisType) idr.Close(); } // End Using idr return(lsReturnValue); } // End Function GetList
} // End Function GetSetters public static Setter_t <T>[] GetSetters <T>(string[] fieldNames) { Setter_t <T>[] iisLogSetters = new Setter_t <T> [fieldNames.Length]; for (int i = 0; i < fieldNames.Length; ++i) { iisLogSetters[i] = GetSetter <T>(fieldNames[i]); } // Next i return(iisLogSetters); } // End Function GetSetters
} // End Function GetSetters public static Setter_t <T>[] GetSetters <T>(string[] fieldNames) { // System.Action<T, object>[] iisLogSetters = new System.Action<T, object>[fieldNames.Length]; Setter_t <T>[] iisLogSetters = new Setter_t <T> [fieldNames.Length]; for (int i = 0; i < fieldNames.Length; ++i) { iisLogSetters[i] = GetSetter <T>(fieldNames[i]); } return(iisLogSetters); } // End Function GetSetters
//public static System.Action<T, object> GetSetter<T>(string fieldName) public static Setter_t <T> GetSetter <T>(string fieldName) { // Class in which to set value System.Linq.Expressions.ParameterExpression targetExp = System.Linq.Expressions.Expression.Parameter(typeof(T), "target"); // Object's type: System.Linq.Expressions.ParameterExpression valueExp = System.Linq.Expressions.Expression.Parameter(typeof(object), "value"); // Expression.Property can be used here as well System.Linq.Expressions.MemberExpression memberExp = null; try { // memberExp = System.Linq.Expressions.Expression.Field(targetExp, fieldName); // memberExp = System.Linq.Expressions.Expression.Property(targetExp, fieldName); memberExp = System.Linq.Expressions.Expression.PropertyOrField(targetExp, fieldName); } catch (System.Exception ex) { return(null); } // http://www.dotnet-tricks.com/Tutorial/linq/RJX7120714-Understanding-Expression-and-Expression-Trees.html System.Linq.Expressions.ConstantExpression targetType = System.Linq.Expressions.Expression.Constant(memberExp.Type); // http://stackoverflow.com/questions/913325/how-do-i-make-a-linq-expression-to-call-a-method System.Linq.Expressions.MethodCallExpression mce = System.Linq.Expressions.Expression.Call(m_FlexibleChangeType, valueExp, targetType); //System.Linq.Expressions.UnaryExpression conversionExp = System.Linq.Expressions.Expression.Convert(valueExp, memberExp.Type); System.Linq.Expressions.UnaryExpression conversionExp = System.Linq.Expressions.Expression.Convert(mce, memberExp.Type); System.Linq.Expressions.BinaryExpression assignExp = // System.Linq.Expressions.Expression.Assign(memberExp, valueExp); // Without conversion System.Linq.Expressions.Expression.Assign(memberExp, conversionExp); // System.Action<TTarget, TValue> setter = System.Linq.Expressions.Expression // System.Action<T, object> setter = System.Linq.Expressions.Expression // .Lambda<System.Action<T, object>>(assignExp, targetExp, valueExp).Compile(); Setter_t <T> setter = System.Linq.Expressions.Expression .Lambda <Setter_t <T> >(assignExp, targetExp, valueExp).Compile(); return(setter); } // End Function GetGetter
} // End Function RemoveSingleLineSqlComments public virtual T GetClass <T>(System.Data.IDbCommand cmd, T tThisClassInstance) { System.Type t = typeof(T); lock (cmd) { using (System.Data.IDataReader idr = ExecuteReader(cmd)) { lock (idr) { while (idr.Read()) { int iFieldCount = idr.FieldCount; Setter_t <T>[] mems = new Setter_t <T> [iFieldCount]; for (int i = 0; i < iFieldCount; ++i) { string strName = idr.GetName(i); mems[i] = LinqHelper.GetSetter <T>(strName); } // Next i for (int i = 0; i < idr.FieldCount; ++i) { Setter_t <T> setter = mems[i]; if (setter != null) { object objVal = idr.GetValue(i); setter(tThisClassInstance, objVal); } } // Next i break; } // Whend idr.Close(); } // End Lock idr } // End Using idr } // End lock cmd return(tThisClassInstance); } // End Function GetClass