1

am trying to convert a list of objects to datatable and am using the solution given in this response https://stackoverflow.com/a/5805044/1447718.

i downloaded hyperproperty and recompiled it to 4.5.2 and used that in my application. when i execute the method, am getting empty dataset with one column. On debugging, i found that the line

PropertyDescriptorCollection properties =
            TypeDescriptor.GetProperties(typeof(T));

is giving properties object with count 0.
i tried replacing the line with

PropertyDescriptorCollection properties =
            TypeDescriptor.GetProperties(data.First().GetType()); 

still no luck.

can anyone help? thanks.

user1447718
  • 669
  • 1
  • 11
  • 23
  • I can almost certainly help, but I really need to see the type you're using here. For example, does it actually have properties? (Fields and properties are different things). To be explicit: I want to see the `Foo` where `Foo` is the argument for the generic type parameter `T` – Marc Gravell Mar 26 '20 at 21:38
  • thanks Marc. the type is public class RequestData { public string d; public DataType t; public int i; } and i wilil be having IList and DataType is a enum. – user1447718 Mar 26 '20 at 21:40

1 Answers1

1

From comments:

public class RequestData {
    public string d;
    public DataType t;
    public int i;
}

These are fields, not properties. The PropertyDescriptor model focuses on properties, and frankly public fields are just an anti-pattern. Consider making these into properties. At the simplest, just add {get;set;} after each, and you're done.

public class RequestData {
    public string d {get;set;}
    public DataType t {get;set;}
    public int i {get;set;}
}

Personally I'd rename them to be more meaningful, but that won't change how they work.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • btw, does HyperPropertyDescriptor work with .net 4.5.2 framework? – user1447718 Mar 26 '20 at 21:44
  • 1
    @user1447718 you'd have to look back in nuget history to find the last version that actually targeted net452; the problem is that net452 is overhead to support - for a longer version of my thoughts here: https://blog.marcgravell.com/2020/01/net-core-net-5-exodus-of-net-framework.html – Marc Gravell Mar 26 '20 at 21:48
  • This fixed my issue. Thanks for your timely help Marc. didnt know this was in nuGet, let me try to get. also i see a fix by mattsmiller here . https://www.codeproject.com/Messages/4510470/Re-Stackoverflow-Exception.aspx do i need to copy it to avoid any overflow issue? – user1447718 Mar 26 '20 at 21:59
  • @user1447718 I'm not going to speculate on a 7 year old comment. Are you seeing a problem? If so: log a repro on github. – Marc Gravell Mar 26 '20 at 22:06
  • no, am not seeing any issue. just check. after your note, i downloaded hyper from nuGet and tried to use it. it is working fine. my client is using .net 4.5.2. this is good for me. Thanks. one last question is does the same library help to bulk insert the values in datatable to a specified table in db? i have 10000 rows X 10 columns that i want to insert to db. – user1447718 Mar 26 '20 at 22:11
  • 1
    @user1447718 yes, there is a `SqlBulkCopy` example on the GitHub home page: https://github.com/mgravell/fast-member#ever-needed-an-idatareader – Marc Gravell Mar 26 '20 at 22:13
  • Hi Marc, the git url that you gave is pointing to FastMember for sqlbulkcopy. am using hyperproperty. is it possible to do with Hyperproperty? – user1447718 Mar 27 '20 at 04:28