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

发布于 2024-08-31 16:58:27 字数 936 浏览 4 评论 0 原文

我需要一种合理的方法将任意文本文件绘制到 C# 程序中,并生成任意匿名类型对象,或者可能是某种复合字典。

我有一个具有代表性的文本文件,如下所示:

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

有没有办法将此信息放入匿名类型对象或某种类似的结构中?

如果它是用 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"
    }
}

请注意,由于文本文件的内容是任意的(即键可以是任何东西),因此需要专门的解决方案(例如查找诸如“FactoryIEEE”之类的名称)将不起作用。但是,文件的结构始终是相同的(即组缩进、冒号和逗号作为分隔符等)。

或者也许我的处理方式是错误的,你有更好的主意吗?

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.

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:

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 (i.e. the keys could be anything), a specialized solution (e.g. that looks for names like "FactoryIEEE") won't work. However, the structure of the file will always be the same (i.e. 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

少跟Wǒ拽 2024-09-07 16:58:27

你的处理方式是错误的。您的“匿名类型对象”数据很难构造并且很难使用。要构建它,您必须使用反射技巧。为了什么?

考虑一个 PrintReport 函数。由于 ATO 的使用,这个函数不会变得更简单。远非如此,它会变得更加复杂和缓慢,必须使用反射本身来迭代键。如果可能的键数量较小且固定,那么您的解决方案可能有意义。那么诸如“obj.FactoryIEEE”之类的使用语法可能是首选。

我解决这个问题的方法是使用 List>,或者说 List 其中 AdapterRecord 是

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

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. This function would not get any simpler because of the ATO usage. 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.

The way I'd go about this is with a List<Dictionary<string, string>>, or, say a List<AdapterRecord> where AdapterRecord is

class AdapterRecord
{
    public string Name { get; set; }
    public Dictionary<string, string> Parameters { get; set; }
}
你的背包 2024-09-07 16:58:27

查看这篇由两部分组成的文章。它以流畅的方式解析 XML 文件。我认为它可以适应解析您的文本文件。

它使用 C# 4 动态类型来完成您想要的操作。

Check out this 2-part article. It parses XML files in a fluent way. It can be adapted to parse your text files I think.

It uses C# 4 dynamic typing to do what you want.

2024-09-07 16:58:27

看起来即使在 C# 4 中也无法做到这一点。

此外,考虑一下如果您不知道键是什么,那么任何类型的强类型访问都会很困难,并且简单的拆分 + 插入字典/清单/等等。会更有意义。

It looks like there's no way to do this even in 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. would make more sense.

遥远的她 2024-09-07 16:58:27

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

While I don't love them, perhaps a DataTable would serve...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文