Link to home
Start Free TrialLog in
Avatar of Member_4438002
Member_4438002

asked on

Cast List<Object> to List<MyClass>

Hi,

I have to write an ADO.Net Context, and rather than repetitively setting properties for each object, I decided to have a function that is passed an object as a template, opens a reader, then iterates through the properties of the object, setting them to the corresponding reader value.

This works absolutely great, however I'm not sure how to cast the returned List<object> to List<MyClass>

Here's the code:

Convert to List
public static List<object> ConvertToList(string SQL, Dictionary<string, dynamic> Parameters, string ConnectionString, object template)
        {
            SqlConnection conn = new SqlConnection(ConnectionString);
            IDbCommand cmd = conn.CreateCommand();
            List<object> ReturnList = new List<object>();
            Type convertType = template.GetType();
            DataTable dt = new DataTable();

            conn.Open();

            cmd.CommandText = SQL;
            cmd.CommandType = CommandType.StoredProcedure;


            //add parameters
            if (Parameters != null)
            {
                foreach (KeyValuePair<string, dynamic> prop in Parameters)
                {
                    IDataParameter param = cmd.CreateParameter();
                    param.ParameterName = prop.Key;
                    param.Value = prop.Value;

                    cmd.Parameters.Add(param);
                }
            }

            IDataReader reader = cmd.ExecuteReader();

            //dt.Load(reader);

            while (reader.Read())
            {
                object obj = Activator.CreateInstance(convertType);

                foreach (PropertyInfo propInfo in convertType.GetProperties())
                {
                    if (propInfo.CanRead)
                    {
                        dynamic val = null;

                        if (propInfo.PropertyType == typeof(string))
                        {
                            val = reader[propInfo.Name].ToString();
                        }
                        else if (propInfo.PropertyType == typeof(int))
                        {
                            val = Convert.ToInt32(reader[propInfo.Name]);
                        }
                        else if (propInfo.PropertyType == typeof(bool))
                        {
                            val = Convert.ToBoolean(reader[propInfo.Name]);
                        }
                        else if (propInfo.PropertyType == typeof(decimal))
                        {
                            val = Convert.ToDecimal(reader[propInfo.Name]);
                        }
                        else if (propInfo.PropertyType == typeof(DateTime))
                        {
                            val = Convert.ToDateTime(reader[propInfo.Name]);
                        }
                        else if (propInfo.PropertyType == typeof(Guid))
                        {
                            val = (Guid)reader[propInfo.Name];
                        }
                        else
                        {
                            Console.WriteLine("here");
                        }

                        convertType.GetProperty(propInfo.Name).SetValue(obj, val, null);
                    }
                }

                ReturnList.Add(obj);
            }

            reader.Close();
            conn.Close();

            return ReturnList;
        }

Open in new window


Set to Defined List
List<User> u = Extensions.ConvertToList("spListUsers", parameters, ConnectionString, new User());
            

Open in new window


Set to Defined list is the bit that isn't working



I've tried several things, but none seem to work.  Any ideas?
ASKER CERTIFIED SOLUTION
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Member_4438002
Member_4438002

ASKER

Never mind, I have fixed it

Here it is incase anyone is curious

List<User> users = u.Select(f => (User)f).ToList();

Open in new window

I've requested that this question be closed as follows:

Accepted answer: 0 points for krapvag's comment #a39200952

for the following reason:

found solution myself
sorry, just saw this.  I will accept this answer