Cannot implicitly convert type 'System.Collections.IList' to 'System.Collections.Generic.List

This is my first attempt at generics and I'm a little puzzled.  I don't understand what is causing this error.  I've googled this and so far no light bulb going off.  I've pointed out the line that causes the error but I can't seem to figure out why.  I've included the functions related so you can get the overall picture to see what I'm doing.  some insight would be appreciated.  also being the first attempt at generics, I'm not sure if I'm doing this right, so feed back is welcomed.


The error

Cannot implicitly convert type 'System.Collections.IList' to 'System.Collections.Generic.List<Domains.Entities.Employee>'


Error point
this is the Load event of a form called debug

private void Debug_Load(object sender, EventArgs e)
        {
            SQL_DataObject empData = new SQL_DataObject();
            
            Employee objemp = new Employee();

              [b]the Error line:[/b]    List<Employee> Lst_rcds = empData.Get_Data<Employee>(objemp);
        }

Open in new window




My DataObject Class:

 
public System.Collections.IList Get_Data<T>(object objentity)
        {
        
            string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

            using (SqlConnection con = new SqlConnection(cs))
            {
                SqlCommand cmd = new SqlCommand("SPGetStudentByID", con);
                cmd.CommandType = CommandType.StoredProcedure;
                //SqlParameter param = new SqlParameter("@ID", ID);
                //cmd.Parameters.Add(param);

                List<T> lst_entity = new List<T>();

                con.Open();
                SqlDataReader rdr = cmd.ExecuteReader();

                while (rdr.Read())
                {
                    //lst_entity.Add(Set_EntityFields(objentity));

                }
                
                return  lst_entity.ToList();
            }
              throw new NotImplementedException();
        }


private object Set_EntityFields<T>(T objentity)
        {
            setInstanceProperty<object>(objentity, "ID", 123);
            
            return null;
            
        }



static void setInstanceProperty<T>(object instance, string propertyName, T value)
        {
            Type type = instance.GetType();
            PropertyInfo propertyInfo = type.GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public, null, typeof(T), new Type[0], null);

            propertyInfo.SetValue(instance, value, null);

            return;
        }

Open in new window

Steve7423Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
Instead of
public System.Collections.IList Get_Data<T>(object objentity)
try
public System.Collections.List Get_Data<T>(object objentity)

Note the second returns a List not an IList (= Interface)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Miguel OzSoftware EngineerCommented:
The method needs to return strong type generic list. (Thus Ilist or List will not return the desired List<Employee>)
Replace:
public System.Collections.IList Get_Data<T>(object objentity)
with
public List<T> Get_Data<T>(T objentity)

Open in new window

Steve7423Author Commented:
AndyAinscow, Miguel Oz,


Thank you this solved it.  I can't believe I over looked this, ...forest through the trees I guess.

Miguel Oz, I will be granting the answer to AndyAinscow, since his response was correct and the first.  I made the change, removing System.Collections.IList and setting it to List<T>.  then, resolved another unrelated error and it worked.

Thank you both for directing my focal point to this difference.
Steve7423Author Commented:
Quick and accurate response.  Thank you
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.