[英]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.