使用自动生成的代码而不预先生成?

发布于 2024-12-02 02:30:02 字数 1010 浏览 1 评论 0原文

是否可以在生成的同一运行中使用自动生成的类?我正在尝试实现一些可以作为工厂工作但适用于自动生成的类的东西。我的场景是这样的:

我有一些文件指定了一些结果文件可以包含的内容。然后,这些结果文件应该用于计算统计数据或其他内容。现在我想在读取这些数据时修改数据,但由于软件不知道哪些部分写入文件,因此它不知道如何在内部将数据保存在文件中。我有一个阅读器和作家(由朋友制作,非常流派,使用起来太麻烦)。我想我可以使用这些定义来自动生成类/结构。

例如: 可写的类有“A”、“B”和“C”。假设“A”和“B”分别有两个和三个孩子。我们称这些为 A0、A1、B0、B1、B2。

现在在软件内部,我想写:

A aObject = new A0();
aObject.variable1 = 2;
aObject.variable2 = "something";

B bObject = new B1();
bObject.SomeVariable = 42;
bObject.SomeOtherVariable = "someValue";

变量的名称以及数据代表的类型都写在规范文件中。例如,在 xml 中,它会是这样的:(

<A0>
<Variable>
   <Name>variable1</Name>
   <type>int</type>
</Variable>
<Variable>
   <Name>variable2 </Name>
   <type>string</type>
</Variable>
</A0>

与 A1、B0、B1、B2 等相同)

现在,我想读取结果文件,意识到我必须自动生成 A0,生成它,修改数据(使用刚刚创建的类/结构 A0),然后再次写入文件。

我的解决方案是预先生成所有可能的类/结构并包含它们。问题在于 A、B 和 C 的版本正在或将会发生变化。

感谢所有帮助:)

Is it possible to use an autogenerated class in the same run as it is being generated? I am trying to achieve something which works as a factory but for autogenerated classes. My scenario is like this:

I have files which specify what some resulting files can contain. These result files should then be used to calculate statistics or something other than that. Now I would like to modify the data when these are read but since the software does not know what parts are written to file, it does not know how to save the data in the files internally. I have a reader and writer (made by a friend and which are VERY genreric and too cumbersome to use). I figured that I could use the definitions to autogenerate classes/structs.

for instance:
classes which can be written are "A", "B" and "C". Say that "A" and "B" have two and three children respectively. Lets call these A0, A1, B0, B1,B2.

Now internally in the software, I would like to write:

A aObject = new A0();
aObject.variable1 = 2;
aObject.variable2 = "something";

B bObject = new B1();
bObject.SomeVariable = 42;
bObject.SomeOtherVariable = "someValue";

what the variables are called and what type the data represents is written in a specification file. In xml for instance, it would be something like:

<A0>
<Variable>
   <Name>variable1</Name>
   <type>int</type>
</Variable>
<Variable>
   <Name>variable2 </Name>
   <type>string</type>
</Variable>
</A0>

(same with A1, B0, B1, B2 and so on)

Now, I would like to read a result file, realize I would have to autogenerate A0, generate it, modify the data (with the class/struct A0 which was just created) and then write to file again.

My solution for this is to pregenerate all possible classes/structs beforehand and include them. Problem with this is that the versions of A, B and C are or will be changing.

All help is appreciated :)

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

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

发布评论

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

评论(1

屋檐 2024-12-09 02:30:02

老实说,你的其余问题对我来说没有多大意义,但要回答你的第一个问题:是的,你可以做到这一点。例如,看看这里

问候格特-扬

The rest of your question did not make all that much sense to me to be honest, but to answer your very first question: yes you can do that. Have a look here for instance.

Regards Gert-Jan

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