简体   繁体   English

从任意文本文件创建匿名类型对象

[英]Create an anonymous type object from an arbitrary text file

I need a sensible way to draw arbitrary text files into a C# program, and produce an arbitrary anonymous type object, or perhaps a composite dictionary of some sort. 我需要一种明智的方法来将任意文本文件绘制到C#程序中,并生成任意匿名类型对象,或者可能是某种复合字典。

I have a representative text file that looks like this: 我有一个代表性的文本文件,如下所示:

adapter 1: LPe11002
  Factory IEEE: 10000000 C97A83FC
  Non-Volatile WWPN: 10000000 C93D6A8A , WWNN: 20000000 C93D6A8A
adapter 2: LPe11002
  Factory IEEE: 10000000 C97A83FD
  Non-Volatile WWPN: 10000000 C93D6A8B , WWNN: 20000000 C93D6A8B

Is there a way to get this information into an anonymous type object or some similar structure? 是否有办法将这些信息转换为匿名类型对象或某些类似的结构?

The final anonymous type might look something like this, if it were composed in C# by hand: 如果最终的匿名类型是由C#手工编写的,则它可能看起来像这样:

new
{
    adapter1 = new 
    { 
        FactoryIEEE = "10000000 C97A83FC",
        Non-VolatileWWPN = "10000000 C93D6A8A",
        WWNN = "20000000 C93D6A8A"
    }
    adapter2 = new 
    { 
        FactoryIEEE = "10000000 C97A83FD",
        Non-VolatileWWPN = "10000000 C93D6A8B",
        WWNN = "20000000 C93D6A8B"
    }
}

Note that, as the text file's content is arbitrary (ie the keys could be anything ), a specialized solution (eg that looks for names like "FactoryIEEE") won't work. 请注意,由于文本文件的内容是任意的(即,键可以是任何东西 ),所以专用的解决方案(例如,查找名称为“ FactoryIEEE”的名称)将不起作用。 However, the structure of the file will always be the same (ie indentation for groups, colons and commas as delimiters, etc). 但是,文件的结构将始终相同(即,组的缩进,分隔符的冒号和逗号等)。

Or maybe I'm going about this the wrong way, and you have a better idea? 或者,也许我走错路了,而您有一个更好的主意?

You're going about this the wrong way. 您正在以错误的方式进行操作。 Your "anonymous type object" data is hard to construct and hard to use . 您的“匿名类型对象”数据很难构造使用 To construct it, you'd have to use reflection trickery. 要构建它,您必须使用反射技巧。 And for what? 为了什么

Consider a PrintReport function. 考虑一个PrintReport函数。 This function would not get any simpler because of the ATO usage. 由于使用了ATO,此功能将变得更加简单。 Far from it, it'd get more complicated and slow, having to use reflection itself to iterate over the keys. 远非如此,它将变得更加复杂和缓慢,必须使用反射本身来迭代键。 Your solution might have made sense if there was a small, fixed number of possible keys. 如果可能的密钥数量很少且固定,则您的解决方案可能很有意义。 Then usage syntax such as "obj.FactoryIEEE" might have been preferred. 然后,可能首选使用用法语法,例如“ obj.FactoryIEEE”。

The way I'd go about this is with a List<Dictionary<string, string>> , or, say a List<AdapterRecord> where AdapterRecord is 我要使用的方法是使用List<Dictionary<string, string>> ,或者说一个List<AdapterRecord> ,其中AdapterRecord是

class AdapterRecord
{
    public string Name { get; set; }
    public Dictionary<string, string> Parameters { get; set; }
}

Check out this 2-part article. 查看这篇由两部分组成的文章。 It parses XML files in a fluent way. 它以一种流畅的方式解析XML文件。 It can be adapted to parse your text files I think. 我认为它可以适应解析您的文本文件。

It uses C# 4 dynamic typing to do what you want. 它使用C#4动态类型执行所需的操作。

It looks like there's no way to do this even in C# 4. 看起来即使在C#4中也无法执行此操作。

Besides, thinking about it if you don't know what the keys will be then any sort of strongly typed access would be tough, and a simple split + insertion into dictionary/list/etc. 此外,如果您不知道键是什么,请考虑一下,然后很难进行任何类型的强类型访问,并且只需简单地将split +插入到dictionary / list / etc中即可。 would make more sense. 会更有意义。

虽然我不喜欢它们,但也许可以使用DataTable ...

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM