DataTable from FileHelpers Class - type.GetProperties () returns an empty array

I successfully pulled the CSV file into the following class:

[DelimitedRecord(",")] [IgnoreFirst(1)] // ignores first line of file, since it a header public class Employee { public string EmployeeId; public string FirstName; public string LastName; // etc. } 

I need to create a DataTable based on this class in order to use SqlBulkCopy. I found some examples, but the following method does not work for me:

 private static DataTable createEmptyDataTable(Type myType) { DataTable dt = new DataTable(); foreach (PropertyInfo info in myType.GetProperties()) { dt.Columns.Add(new DataColumn(info.Name, info.PropertyType)); } return dt; } 

The problem is myType.GetProperties (). This is not an error, but returns nothing. The PropertyInfo array that it should return is empty. Was at this time and can not understand the problem ...

EDIT: I also used this option without success:

 private static DataTable createEmptyDataTable(Type myType) { DataTable dt = new DataTable(); PropertyInfo[] infoArray = myType.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static); foreach (PropertyInfo info in infoArray) { dt.Columns.Add(new DataColumn(info.Name, info.PropertyType)); } return dt; } 
+4
source share
3 answers

The Employee class contains fields, not properties. Use soon

 myType.GetFields() 
+17
source

When working with Properties: You need to specify the scope for Get Properties, try to return all types of properties: GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);

When working with fields (i.e. get / set) its similar, just another function. See: http://msdn.microsoft.com/en-us/library/ch9714z3.aspx

+5
source

You have no properties in this fragment of the class. I am using this code for Reference.cs classes from web services.

 Employee objE = new Employee(); var members = objE.GetType().GetFields().Select(m => new { Name = m.Name, MemType = m.MemberType, RtField = m.GetType(), Type = m.FieldType, MemAtt = m.GetCustomAttributes(true) }); 
0
source

Source: https://habr.com/ru/post/1484723/


All Articles