Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass generic to the extension method

I'm trying to make a generic method to execute a query where I can pass stored proc name and the parameters if any .

After executing the query the results are stored in a DataTable which has to be converted to a List.

DataTableToList()

is an extension method which will do the same.

Only relevant code shown

Caller

        var results=  dh.ExecuteDataSet<EmployeeModel>("USP_GetEmployeeByID", new Dictionary<string, IConvertible>()
        {
                 {"@ID", 1000}
             });

DAL code

public IEnumerable<T>  ExecuteDataSet<T>(string storedProcName, IDictionary<string, IConvertible> parameters = null)
            {                            
                    var result = db.ExecuteDataSet(q);

                    DataTable dtResult = result.Tables[0];

                    var t = dtResult.DataTableToList<T>();  //Compile time error: The type T must be a reference type in order to use it as parameter 

                        return t;

                }

Extension method

public static List<T> DataTableToList<T>(this DataTable table) where T : class, new()
        {
            try
            {
                List<T> list = new List<T>();

                foreach (var row in table.AsEnumerable())
                {
                    T obj = new T();

                    foreach (var prop in obj.GetType().GetProperties())
                    {
                        try
                        {
                            PropertyInfo propertyInfo = obj.GetType().GetProperty(prop.Name);
                            propertyInfo.SetValue(obj, Convert.ChangeType(row[prop.Name], propertyInfo.PropertyType), null);
                        }
                        catch
                        {
                            continue;
                        }
                    }

                    list.Add(obj);
                }

                return list;
            }
            catch
            {
                return null;
            }
        }

The problem is that the extension method call gives the compile time error

The type T must be a reference type in order to use it as parameter compile time error.

So what changes are to be made to the extension method so that it accepts a generic as an argument?

like image 863
Codebadger Avatar asked Nov 19 '25 16:11

Codebadger


2 Answers

This procedure:

public IEnumerable<T>  ExecuteDataSet<T>(
    string storedProcName,
    IDictionary<string, IConvertible> parameters = null)

Also needs the type parameters.

where T : class, new()
like image 124
Jim Avatar answered Nov 21 '25 05:11

Jim


Add where T : class to your DAL method. The compiler needs to know that your T in the DAL method can satisfy the type constraints of the extension method

like image 25
Stewart Parry Avatar answered Nov 21 '25 07:11

Stewart Parry