locked
get data from T RRS feed

  • Question

  • User-1045082127 posted

    hi

    list of data is send to the bellow fuction:

    public static DataTable SetPropertyToDataTable<T>(T dataList,Type _class) where T : class
            {
                Dictionary<string, object> columns = new Dictionary<string, object>();
    
                var data = dataList as T[];
                var properties =_class.GetProperties(); 
    
               /* foreach (var item in T)
                    foreach (var property in properties)
                    {
                        var name = property.Name;
                        var value = property.GetValue(item, null);
    
                        if(!columns.ContainsKey(name))
                            columns.Add(name, value);
                    }*/
    
                return ImportPropertyAndValues(columns); 
            }

    bu when i trace a data value  it contain null. why?

    Sunday, October 21, 2018 11:12 AM

All replies

  • User303363814 posted

    The signature of SetPropertyToDataTable tells us that the type of dataList is T.

    The 'as' operator says "if dataList is of type T[] then return a value whose type is T[], otherwise return null".

    datalist is not if type T[] so the 'as' operator returns null.  It is impossible for something to be of a certain type and also be an array of that same type at the same time.

    Sunday, October 21, 2018 10:37 PM