简体   繁体   English

如何提取 class object 的 System.Reflection.PropertyInfo 名称作为 csv 文件的第一行

[英]How to extract System.Reflection.PropertyInfo names of a class object as first row of a csv file

I'm trying to use System.Reflection to output a first row of column header information for a csv text file before I create the actual generic List from my data source.我正在尝试使用 System.Reflection 到 output 列 header 信息的第一行,然后从我的数据创建实际的通用列表文本文件。

public class DocMetaData
{
    public const string SEPARATOR = "\t";       // horizontal tab is delimiter
    public string Comment { get; set; }
    public string DocClass { get; set; }
    public string Title { get; set; }
    public string Folder { get; set; }
    public string File { get; set; }
}

In the following routine, I am trying to loop through the properties of the object definition and use the property name as a "column name" for my first row of my output file:在以下例程中,我试图遍历 object 定义的属性,并将属性名称用作我的 output 文件的第一行的“列名”:

private void OutputColumnNamesAsFirstLine(StreamWriter writer)
    {
        StringBuilder md = new StringBuilder();
        PropertyInfo[] columns;
        columns = typeof(DocMetaData).GetProperties(BindingFlags.Public |
                                                      BindingFlags.Static);
        foreach (var columnName in columns)
        {
            md.Append(columnName.Name); md.Append(DocMetaData.SEPARATOR);
        }
        writer.WriteLine(md.ToString());
    }

The foreach loop exits immediately. foreach 循环立即退出。 Also, I put a constant separator in the class but I want to use that as a field separator value (rather than as a "column" name).另外,我在 class 中放置了一个常量分隔符,但我想将其用作字段分隔符值(而不是“列”名称)。

I am assuming that the ordinal position of the properties in the class will be maintained consistently if I can get something like this to work.我假设 class 中属性的序号 position 将保持一致,如果我能得到这样的工作。

The remainder of the code which creates the List<DocMetaData> from my data source works but I'd like to add this "first row" stuff.从我的数据源创建List<DocMetaData>的其余代码可以工作,但我想添加这个“第一行”的东西。

Thank you for help on this.感谢您对此的帮助。

Don't use BindingFlags.Static because that yields only static members (public static).不要使用BindingFlags.Static因为这只会产生 static 成员(公共静态)。 Use BindingFlag.Instance instead, since your properties are instance members.请改用BindingFlag.Instance ,因为您的属性是实例成员。

I suppose you have to do我想你必须做

columns = typeof(DocMetaData).GetProperties(BindingFlags.Public |
                                                      BindingFlags.Instance);

The fields you are trying to search are instance fields not static您尝试搜索的字段是instance字段而不是static

You should replace BindingFlags.Static with BindingFlags.Instance . The properties in your您应该将BindingFlags.Static替换为 BindingFlags.Instance . The properties in your . The properties in your DocMetaData` are not static. . The properties in your不是 static。

private void OutputColumnNamesAsFirstLine(StreamWriter writer)
{
    StringBuilder md = new StringBuilder();
    PropertyInfo[] columns;
    columns = typeof(DocMetaData).GetProperties(BindingFlags.Public |
                                                  BindingFlags.Instance);
    foreach (var columnName in columns)
    {
        md.Append(columnName.Name); 
        md.Append(DocMetaData.SEPARATOR);
    }
    writer.WriteLine(md.ToString());
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将System.Reflection.PropertyInfo对象转换为其原始对象类型 - How to convert System.Reflection.PropertyInfo object to its original object type 如何最好地处理system.reflection.propertyInfo NULLReferenceExceptions - how to best handle system.reflection.propertyInfo NULLReferenceExceptions 无法将类型&#39;System.Reflection.PropertyInfo&#39;转换为 - Cannot convert type 'System.Reflection.PropertyInfo' to 转换表达式数组 <Func<TModel, object> &gt; []到列表列表 <System.Reflection.PropertyInfo> - Converting array of Expression<Func<TModel, object>>[] to list of List<System.Reflection.PropertyInfo> 如何创建列表 <unknown type at compile time> 并通过System.Reflection.PropertyInfo复制项目 - How to create a List<unknown type at compile time> and copy items via System.Reflection.PropertyInfo 运算符&#39;==&#39;不能应用于操作数System.Reflection.PropertyInfo和&#39;int&#39; - Operator '==' cannot be applied to operands System.Reflection.PropertyInfo and 'int' 反思:如何通过 PropertyInfo 获取类对象 - Reflection: How to get the class object via PropertyInfo 'System.String' 类型的表达式不能用于'System.Reflection.PropertyInfo' 类型的参数 - 'Expression of type 'System.String' cannot be used for parameter of type 'System.Reflection.PropertyInfo' System.Reflection-如何获取子类型的PropertyInfo? - System.Reflection - How can I get the PropertyInfo of the sublevel Types? System.Reflection.Emit将代码添加到propertyInfo - System.Reflection.Emit adding code to propertyInfo
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM