c# - get properties using reflections for generic type object -


i having generic class in have function properties of generic object passed.it below.

public class exporttocsv<t>                 t: class {     public exporttocsv(list<t> obj)     {             this.data = obj;            }      public stringbuilder createrows()    {        ienumerable<propertyinfo> properties = typeof(t).getproperties();    } } 

it works fine , returns me properties if pass object selecting object(class) below

//getleadingroutingrecords returns class/object var result = obj in getleadroutingrecords()                     select new                     {                         leadroutingid = obj.leadroutingid,                         make = obj.make                      }; 

and pass result result.tolist();

but when try create own anonymous object creating class properties below doesn't work not returning properties

note : below code being called in loop , functioning , being passed above function can see values debugging.

public csvreport function return(){     return new csvreport                 {                     shopname = this.val,                     targetvehiclename = val                  }.tolist(); } 

class wrote above anonymous object below :

public class csvreport     {         public string shopname { get; set; }         public string targetvehiclename { get; set; }     } 

so in case not working, selecting first record , getting properties below

this.data.first().gettype().getproperties(); 

i want use first pattern here, type(t).getproperties

so, work around please........................

reflection on typeof(t) works fine; here's simpler example based on yours, (importantly) runnable. outputs:

shopname targetvehiclename 

code:

using system; using system.collections.generic; public class csvreport {     public string shopname { get; set; }     public string targetvehiclename { get; set; } } class exporttocsv<t> {     list<t> data;     public exporttocsv(list<t> obj)     {         data = obj;     }     public void writepropnames()     {         foreach (var prop in typeof(t).getproperties())         {             console.writeline(prop.name);         }     }  } static class program {     static void main()     {         var obj = new list<csvreport>();         obj.add(new csvreport { shopname = "foo", targetvehiclename = "bar" });         new exporttocsv<csvreport>(obj).writepropnames();     } } 

Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

jquery - How would i go about shortening this code? And to cancel the previous click on click of new section? -