C# FileHelpers类中的DataTable--type.GetProperties()返回空数组

C# FileHelpers类中的DataTable--type.GetProperties()返回空数组,c#,.net,filehelpers,C#,.net,Filehelpers,我已成功将CSV文件拉入以下类: [DelimitedRecord(",")] [IgnoreFirst(1)] // ignores first line of file, since it's a header public class Employee { public string EmployeeId; public string FirstName; public string LastName; // etc. } 为了使用SqlBulkCopy,

我已成功将CSV文件拉入以下类:

[DelimitedRecord(",")]
[IgnoreFirst(1)] // ignores first line of file, since it's a header
public class Employee {
    public string EmployeeId;
    public string FirstName;
    public string LastName;
    // etc.
}
为了使用SqlBulkCopy,我需要基于该类创建一个DataTable。我发现了几个例子,但以下方法对我不适用:

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;
    }
问题出在myType.GetProperties()上。它不会抛出错误,但不会返回任何内容。它应该返回的PropertyInfo数组为空。我已经在这里呆了一段时间了,但还是不能解决问题

编辑: 我也使用过这种变体,但没有成功:

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;
    }

使用属性时: 您需要指定Get属性的作用域,尝试此操作以返回所有属性类型:
GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static)

当使用字段(即,没有get/set)时,它是相似的,只是一个不同的函数。见:

您的Employee类包含字段,而不是属性。使用

myType.GetFields()

此类代码段中没有任何属性。我将此代码用于web服务中的Reference.cs类

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)
});

什么都没有是什么意思?空数组?这意味着您没有任何公共属性。正如Dominic提到的,你有字段而不是属性你确定你在寻找属性而不是字段吗?改用GetFields。再次感谢你,David。多米尼克几乎没有超过你的建议,所以我给了他的答案,但我真的很感谢你的帮助!是的,他解决了实际问题。祝你在成长过程中好运,我都没注意到。好眼力,多米尼克。