如何在 C# 中使用反射读取平面文件的架构
如何在 C# 中使用反射读取平面文件的架构 我的简要概述是-有一些服务器将数据存储为平面文件,我必须将数据从 SQL Server 推送到这些平面文件。 为此,我需要了解他们的架构。 我对此一无所知,任何帮助都会很棒。
How to Read Schema of Flat File using Reflection in C#
The brief overview that I have is- There's some server which is storing the data as flat files and I have to push data from SQL Server to these flat files. For that I need to know their schema.
I have no idea about this, any help would be great.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
很难知道从哪里开始回答这个问题,因为听起来反射不太可能有帮助,但您指定它必须出现在解决方案中! :)
因此,这个答案主要是猜测,并且可能会随着您透露更多信息而演变。 这可以; 除非你知道问题是什么,否则你无法得到答案 (42)。
对于什么是平面文件没有单一的定义。 您可能需要一个解析文件中“记录”的函数。 通常的方法是在十六进制编辑器中打开文件并从一些示例记录中推断格式。 寻找有关重复图案形状的线索。 每个“记录”似乎都由许多“字段”组成? 它们每次都以明确的顺序出现,还是每个字段都以名称或数字作为前缀来指示其含义? 它看起来像纯文本吗?即您可以在记事本中打开它而不会丢失重要信息吗?
声明一个类来表示一条记录:
然后编写一个从流中读取并生成记录的函数:
如果文件是“纯文本”(即,它具有回车符/换行符),则适合使用 StreamReader以某种重要方式用于分隔记录或字段)。
假设每条记录都是一行纯文本(换句话说,每条记录都由特殊的行结束序列分隔)。 该行中有多个字段,以某种方式由其他特殊字符分隔。 您的解析器可能被构造为循环:
这将问题分为两个级别。 外层一次穿过一条线,每条线产生一条记录。 然后,我想象将一个内部级别放入 FlatFileRecord 的构造函数中。 传递一行,它会用它知道如何从该行提取的字段值填充自己。 所以现在你必须编写 FlatFileRecord 的构造函数。
Hard to know where to start with this question, because it sounds like reflection is unlikely to be helpful, and yet you specified that it must appear in the solution! :)
As a result, this answer is mostly guesswork and will probably evolve as you reveal more information. This is fine; you can't get the answer (42) until you know what the question is.
There isn't a single definition of what a flat file is. What you probably need is a function that parses the "records" in the file. The usual approach is to open the file in a hex editor and infer the format from a few example records. Look for clues as to the shape of the repeating pattern. Does each "record" appear to consist of a number of "fields"? Do they appear in a definite order every time, or is each field prefixed by a name or number to indicate what it means? Does it look like plain text, i.e. can you open it in Notepad without losing important information?
Declare a class to represent a record:
Then write a function that reads from a stream and yields records:
It would be appropriate to use StreamReader if the file is "plain text" (that is, it has carriage-return/line-feed characters that are used in some significant way to delimit records or fields).
Suppose each record is a line of plain text (in other words, each record is separated by a special line-ending sequence). Within that line there are multiple fields, somehow delimited by other special characters. Your parser might be structured as a loop:
This breaks the problem down into two levels. The outer level goes through the lines one at a time, each producing one record. Then there is an inner level that I've imagined putting in the constructor of FlatFileRecord. Pass a line, and it fills itself in with the field values that it knows how to extract from that line. So now you have to write the constructore of FlatFileRecord.