how to create class and List dynamically in C#, for ex:if we pass data-set to method that should return List formatted Data
but on conventional model Each time i need to create class and method, so anybody having ideas pls share it.
Here is my conventional method Code:-
[WebMethod]
    public static List<ICF> ge_Grid_data(string paramstr, string procname)
    {
        #region
        List<ICF> lst = new List<ICF>();
        try
        {
            string[] parameters = paramstr.Split('~');
            string err = string.Empty;
            int len = parameters.Length;
            SqlParameter[] sqlParam = new SqlParameter[len];
            for (int i = 0; i < len; i++)
            {
                string[] paramWithValue = parameters[i].Split('$');
                string param = paramWithValue[0].ToString();
                string value = paramWithValue[1].ToString();
                sqlParam[i] = new SqlParameter { ParameterName = param, Value = value };
            }
            DataSet ds = new clsiCMSBLBase().GetListData(ref err, sqlParam, procname);
            DataTable dt = ds.Tables[0];
            foreach (DataRow dr in dt.Rows)
            {
                ICF obj = new ICF();
                obj.Flag = Convert.ToInt32(dr["Flag"]);
                obj.ClaimID = dr["ClaimID"].ToString();
                obj.RyotNumber = dr["RyotNumber"].ToString();
                obj.SeasonCode = dr["SeasonCode"].ToString();
                obj.PlotNumber = dr["PlotNumber"].ToString();
                obj.RyotNumber = dr["RyotNumber"].ToString();
                obj.RyotName = dr["RyotName"].ToString();
                obj.ClaimDate = dr["ClaimDate"].ToString();
                obj.ClaimFormNo = dr["ClaimFormNo"].ToString();
                obj.ClaimArea = dr["ClaimArea"].ToString();
                obj.ClaimAmount = dr["ClaimAmount"].ToString();
                obj.ClaimReason = dr["ClaimReason"].ToString();
                obj.SurveyorID = dr["SurveyorID"].ToString();
                obj.SurveyorDate = dr["SurveyorDate"].ToString();
                obj.InsuranceAmount = dr["InsuranceAmount"].ToString();
                lst.Add(obj);
            }
        }
        catch (Exception ex)
        {
        }
        return lst;
        #endregion
    }
Here is ICF Class:-
public class ICF
    {
        #region
        public int Flag { get; set; }
        public string ClaimID { get; set; }
        public string SeasonCode { get; set; }
        public string PlotNumber { get; set; }
        public string RyotNumber { get; set; }
        public string RyotName { get; set; }
        public string ClaimDate { get; set; }
        public string ClaimFormNo { get; set; }
        public string ClaimArea { get; set; }
        public string ClaimAmount { get; set; }
        public string ClaimReason { get; set; }
        public string SurveyorID { get; set; }
        public string SurveyorDate { get; set; }
        public string InsuranceAmount { get; set; }
        #endregion
    }
My Expectation:
    public static class Extensions
{
    /// <summary>
    /// Converts datatable to list<T> dynamically
    /// </summary>
    /// <typeparam name="T">Class name</typeparam>
    /// <param name="dataTable">data table to convert</param>
    /// <returns>List<T></returns>
    public static List<T> ToList<T>(this DataTable dataTable) where T : new()
    {
        var dataList = new List<T>();
        //Define what attributes to be read from the class
        const BindingFlags flags = BindingFlags.Public | BindingFlags.Instance;
        //Read Attribute Names and Types
        var objFieldNames = typeof(T).GetProperties(flags).Cast<PropertyInfo>().
            Select(item => new 
            { 
                Name = item.Name, 
                Type = Nullable.GetUnderlyingType(item.PropertyType) ?? item.PropertyType 
            }).ToList();
        //Read Datatable column names and types
        var dtlFieldNames = dataTable.Columns.Cast<DataColumn>().
            Select(item => new { 
                Name = item.ColumnName, 
                Type=item.DataType 
            }).ToList();
        foreach (DataRow dataRow in dataTable.AsEnumerable().ToList())
        {
            var classObj = new T();
            foreach (var dtField in dtlFieldNames)
            {
                PropertyInfo propertyInfos = classObj.GetType().GetProperty(dtField.Name);
                var field = objFieldNames.Find(x => x.Name == dtField.Name);
                if (field != null)
                {
                    if (propertyInfos.PropertyType == typeof(DateTime))
                    {
                        propertyInfos.SetValue
                        (classObj, convertToDateTime(dataRow[dtField.Name]), null);
                    }
                    else if (propertyInfos.PropertyType == typeof(int))
                    {
                        propertyInfos.SetValue
                        (classObj, ConvertToInt(dataRow[dtField.Name]), null);
                    }
                    else if (propertyInfos.PropertyType == typeof(long))
                    {
                        propertyInfos.SetValue
                        (classObj, ConvertToLong(dataRow[dtField.Name]), null);
                    }
                    else if (propertyInfos.PropertyType == typeof(decimal))
                    {
                        propertyInfos.SetValue
                        (classObj, ConvertToDecimal(dataRow[dtField.Name]), null);
                    }
                    else if (propertyInfos.PropertyType == typeof(String))
                    {
                        if (dataRow[dtField.Name].GetType() == typeof(DateTime))
                        {
                            propertyInfos.SetValue
                            (classObj, ConvertToDateString(dataRow[dtField.Name]), null);
                        }
                        else
                        {
                            propertyInfos.SetValue
                            (classObj, ConvertToString(dataRow[dtField.Name]), null);
                        }
                    }
                }                
            }
            dataList.Add(classObj);
        }
        return dataList;
    }
    private static string ConvertToDateString(object date) 
    {
        if (date == null)
            return string.Empty;
        return SpecialDateTime.ConvertDate(Convert.ToDateTime(date));
    }
    private static string ConvertToString(object value)
    {
        return Convert.ToString(HelperFunctions.ReturnEmptyIfNull(value));
    }
    private static int ConvertToInt(object value) 
    {
        return Convert.ToInt32(HelperFunctions.ReturnZeroIfNull(value));
    }
    private static long ConvertToLong(object value)
    {
        return Convert.ToInt64(HelperFunctions.ReturnZeroIfNull(value));
    }
    private static decimal ConvertToDecimal(object value)
    {
        return Convert.ToDecimal(HelperFunctions.ReturnZeroIfNull(value));
    }
    private static DateTime convertToDateTime(object date)
    {
        return Convert.ToDateTime(HelperFunctions.ReturnDateTimeMinIfNull(date));
    }
}
Finally, i need to call it like this:
List<MyClass> list =  dt.ToList<MyClass>
but this code not working
 
     
    