Codebadger
Codebadger

Reputation: 35

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?

Upvotes: 3

Views: 1083

Answers (2)

Jim
Jim

Reputation: 16022

This procedure:

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

Also needs the type parameters.

where T : class, new()

Upvotes: 5

Stewart Parry
Stewart Parry

Reputation: 128

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

Upvotes: 2

Related Questions